-
Notifications
You must be signed in to change notification settings - Fork 46
User-definable abstract types #1198
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -83,8 +83,18 @@ let rec eq_types : (datatype * datatype) -> bool = | |
| fun (t1, t2) -> | ||
| let rec unalias = function | ||
| | Alias (_, _, x) -> unalias x | ||
| | x -> x in | ||
| | x -> x | ||
| in | ||
| match unalias t1 with | ||
| | Abstract abs -> | ||
| begin match unalias t2 with | ||
| | Abstract abs' -> | ||
| Gensym.equal abs abs' | ||
| && (let [@ocaml.warning "-8"]Alias (_, (_, _, tyargs, _), _) = t1 in | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This seems to assume only one layer of aliasing. I guess it is meant to dealwith the problem that if there are type parameters then the Now if I say pass something of type MyAlias(a,b,c) into something expecting MyParameterisedAbstractType(a,b,c) I think I will get a unification error, because we only look at the outer layer of aliasing. I think it would be cleaner to make Abstract take Gensym.t and then a list of type arguments, and if the Gensyms are equal, unify recursively.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK, I will have a look at this. |
||
| let [@ocaml.warning "-8"]Alias (_, (_, _, tyargs', _), _) = t2 in | ||
| List.for_all2 (fun (_tyk, ty) (_tyk', ty') -> eq_types (ty, ty')) tyargs tyargs') | ||
| | _ -> false | ||
| end | ||
| | Not_typed -> | ||
| begin match unalias t2 with | ||
| | Not_typed -> true | ||
|
|
@@ -598,7 +608,18 @@ let rec unify' : unify_env -> (datatype * datatype) -> unit = | |
| Unionfind.change point t; *) | ||
| | t' -> ut (t, t') | ||
| end | ||
| | Alias (_, _, t1), t2 | t1, Alias (_, _, t2) -> ut (t1, t2) | ||
| | Abstract _, _ | _, Abstract _ -> | ||
| failwith "freestanding Abstract (must be under an alias)" | ||
| | Alias (_, _, Abstract abs), Alias (_, _, Abstract abs') -> | ||
| if Gensym.equal abs abs' | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See comment on line 93, this part would hopefully go away if we associate type parameter arguments with Abstract directly rather than piggypacking on Alias. |
||
| then let [@ocaml.warning "-8"]Alias (_, (_, _, tyargs, _), _) = t1 in | ||
| let [@ocaml.warning "-8"]Alias (_, (_, _, tyargs', _), _) = t2 in | ||
| List.iter2 (fun tyargs tyargs' -> unify_type_args' rec_env (tyargs, tyargs')) tyargs tyargs' | ||
| else raise (Failure (`Msg "cannot unify different abstract types")) | ||
| | ((Alias (_, _, Abstract _) as t1), t2) | (t2, (Alias (_, _, Abstract _) as t1)) -> | ||
| raise (Failure (`Msg ("Cannot unify abstract type '" ^ string_of_datatype t1 ^ "' with concrete type '" ^ string_of_datatype t2 ^ "'"))) | ||
| | Alias (_, _, t1), t2 | t1, Alias (_, _, t2) -> | ||
| ut (t1, t2) | ||
| | Application (l, _), Application (r, _) when l <> r -> | ||
| raise (Failure | ||
| (`Msg ("Cannot unify abstract type '"^string_of_datatype t1^ | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| Abstract type declaration | ||
| typename Foo; | ||
| stdout : () : () | ||
|
|
||
| Abstract type unification | ||
| typename Foo; sig foo : (Foo) -> Foo fun foo(x) { x } | ||
| stdout : () : () | ||
|
|
||
| Abstract type identities (1) | ||
| typename Foo; typename Bar; sig foobar : (Foo) -> Bar fun foobar(x) { x } | ||
| stderr : @. | ||
| exit : 1 | ||
|
|
||
| Abstract type identities (2) | ||
| typename Foo; sig foo : () -> Foo fun foo() { error("I cannot materialise a foo") }; typename Foo; sig foo' : (Foo) -> () fun foo'(_) { () }; foo'(foo()) | ||
| stderr : @. | ||
| exit : 1 | ||
|
|
||
| Parameterised abstract types (1) | ||
| typename Foo(a,b,c); sig foo : (Foo(a,b,c)) -> Foo(String,Int,Float) fun foo(x) { x } | ||
| stderr : @. | ||
| exit : 1 | ||
|
|
||
| Parameterised abstract types (2) | ||
| typename Foo(a,b); sig foo : (Foo(Int,String)) -> Foo(Int,String) fun foo(x) { x } | ||
| stdout : () : () | ||
|
|
||
| Parameterised abstract types (3) | ||
| typename Foo(a,b); sig foo : (a, b) ~> Foo(a,b) fun foo(_,_) { error("cannot materialise Foo") } | ||
| stdout : () : () | ||
|
|
||
| Parameterised abstract types (4) | ||
| typename Foo(a,b); sig foo : (Foo(a,b)) ~> Foo(b,a) fun foo(_) { error("cannot materialise Foo") } | ||
| stdout : () : () | ||
|
|
||
| Alien | ||
| typename A; alien javascript "" f : () -> A; | ||
| stdout : () : () |
Uh oh!
There was an error while loading. Please reload this page.