Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect erasure of function types #3366

Open
gebner opened this issue Jul 19, 2024 · 3 comments · May be fixed by #3661
Open

Incorrect erasure of function types #3366

gebner opened this issue Jul 19, 2024 · 3 comments · May be fixed by #3661
Assignees

Comments

@gebner
Copy link
Contributor

gebner commented Jul 19, 2024

(Following up on the discussion we just had.)

F* treats function types with an erasable codomain as erasable themselves. For example, Type -> Type is erasable. If such a function type is hidden behind an interface, then F* will replace values of that function type by () which is incompatible with polymorphism. (In the same file, values of that type are erased to fun _ -> () which is fine.)

//==> FunErasureHelper.fsti <==
module FunErasureHelper

[@@must_erase_for_extraction]
val t : Type u#1

val x : t

val f : t * nat -> nat

//==> FunErasureHelper.fst <==
module FunErasureHelper

let t = Type0 -> Type0

let x = list

let generic_apply #a #b (f: (a -> b) * nat) (x: a) : b * nat =
  (f._1 x, f._2)

let f (x: t * nat) =
  (generic_apply x nat)._2

//==> FunErasure.fst <==
module FunErasure
open FunErasureHelper

let _ = f (x, 42) // <- crash

The last definition will crash, because x is incorrectly erased to (), and generic_apply then treats it as a function and calls it.

@gebner
Copy link
Contributor Author

gebner commented Oct 2, 2024

Options:

  • extend the attribute [@@erasable (fun _ -> ())]
  • extend the attribute with the arity [@@erasable 1] (defaulting to zero) (maybe let erasable = erasable_at 0 or something)

TODO: erase must_erase_for_extraction as well

@gebner
Copy link
Contributor Author

gebner commented Oct 3, 2024

After giving this some more thought, I don't think we can make the arity version of the attribute work. Knowing the arity is enough to detect this issue, but not to produce the right code. During extraction, we still need to know which type to erase to: i.e., should we replace x: t by fun (x: unit) -> () or by fun (x: int) -> (). (ML expressions are typed after all.)

I only see two approaches left:

  1. Specify the erasure replacement in the attribute, i.e., [@@erasable_to (fun (x: unit) -> ())]. You can always avoid the extra boilerplate with an explicit erased.
  2. Enforce CMI extraction, we could then unfold the implementation type. Though we'd still need to fix extraction to erase to fun (x: unit) -> () instead of ().

@nikswamy
Copy link
Collaborator

nikswamy commented Oct 3, 2024

There's a third option. Generate fun (x:Obj.t) -> (). ML expressions can be pretty weakly typed.

gebner added a commit that referenced this issue Oct 18, 2024
This only works if extraction can see through the type definitions,
in general this requires `--cmi`.

Fixes #3366
gebner added a commit that referenced this issue Oct 18, 2024
This only works if extraction can see through the type definitions,
in general this requires `--cmi`.

Fixes #3366
gebner added a commit that referenced this issue Dec 10, 2024
This only works if extraction can see through the type definitions,
in general this requires `--cmi`.

Fixes #3366
@gebner gebner self-assigned this Dec 16, 2024
gebner added a commit that referenced this issue Dec 21, 2024
This only works if extraction can see through the type definitions,
in general this requires `--cmi`.

Fixes #3366
gebner added a commit that referenced this issue Jan 8, 2025
This only works if extraction can see through the type definitions,
in general this requires `--cmi`.

Fixes #3366
@gebner gebner linked a pull request Jan 8, 2025 that will close this issue
gebner added a commit that referenced this issue Jan 10, 2025
This only works if extraction can see through the type definitions,
in general this requires `--cmi`.

Fixes #3366
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants