Skip to content

Commit 3775867

Browse files
committed
Change to attempt to infer wrapped and folded types
1 parent 61e43b1 commit 3775867

10 files changed

Lines changed: 77 additions & 58 deletions

File tree

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -346,9 +346,9 @@ explicitly, and require explicit injection/projection.
346346
```1ml
347347
type stream = rec t => {hd : int, tl : () ~> opt t} ;; creates rec type
348348
single x :@ stream = {hd = x, tl = fun () => none} ;; b :@ t rolls value into t
349-
({hd = n} :@ stream) = single 5 ;; p :@ t pattern matches on rec value
350-
do Int.print n ;; or:
351-
do Int.print (single 7 @: stream).hd ;; e @: t unrolls rec value directly
349+
({hd = n} :@ _) = single 5 ;; p :@ t pattern matches on rec value
350+
do Int.print n ;; or:
351+
do Int.print (single 7 @: _).hd ;; e @: t unrolls rec value directly
352352
```
353353

354354
#### Recursive Functions
@@ -392,8 +392,8 @@ Opt :> OPT = {
392392
;; Church encoding; it requires the abstract type opt a to be implemented
393393
;; with a polymorphic (i.e., large) type. Thus, wrap the type.
394394
type opt a = wrap (b : type) -> b -> (a ~> b) ~> b
395-
none :# opt _ = fun (b : type) (n : b) (s : _ ~> b) => n
396-
some x :# opt _ = fun (b : type) (n : b) (s : _ ~> b) => s x
395+
none :# _ = fun (b : type) (n : b) (s : _ ~> b) => n
396+
some x :# _ = fun (b : type) (n : b) (s : _ ~> b) => s x
397397
caseopt (xo :# opt _) = xo _
398398
}
399399
```

elab.ml

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,8 @@ and paths_row ta ps = function
125125
let rec_from_extyp typ label s =
126126
match s with
127127
| ExT([], t) ->
128-
let rec find_rec = function
128+
let rec find_rec t =
129+
match follow_typ t with
129130
| AppT(t, ts) ->
130131
let rec_t, unroll_t, roll_t, ak = find_rec t in
131132
rec_t, AppT(unroll_t, ts), AppT(roll_t, ts), ak
@@ -399,13 +400,18 @@ Trace.debug (lazy ("[FunE] env =" ^ VarSet.fold (fun a s -> s ^ " " ^ a) (domain
399400
IL.genE(erase_bind aks, IL.LamE(var.it, erase_typ t, e2))
400401

401402
| EL.WrapE(var, typ) ->
403+
let var_t = lookup_var env var in
402404
let s, zs1 =
403405
match elab_typ env typ "" with
404406
| ExT([], WrapT(s)), zs1 -> s, zs1
407+
| ExT([], InferT(z)), zs1 ->
408+
let s = ExT([], var_t) in
409+
resolve_always z (WrapT(s));
410+
s, zs1
405411
| _ -> error typ.at "non-wrapped type for wrap"
406412
in
407413
let _, zs2, f =
408-
try sub_extyp env (ExT([], lookup_var env var)) s []
414+
try sub_extyp env (ExT([], var_t)) s []
409415
with Sub e -> error exp.at
410416
("wrapped type does not match annotation: " ^ Sub.string_of_error e)
411417
in
@@ -499,11 +505,18 @@ Trace.debug (lazy ("[AppE] ts = " ^ String.concat ", " (List.map string_of_norm_
499505
IL.AppE(IL.instE(ex1, List.map erase_typ ts), IL.AppE(f, IL.VarE(var2.it)))
500506

501507
| EL.UnwrapE(var, typ) ->
508+
let t1, zs1, ex = elab_instvar env var in
502509
let aks, t, s2, zs2 =
503510
match elab_typ env typ l with
504511
| ExT([], WrapT(ExT(aks, t) as s2)), zs2 -> aks, t, s2, zs2
512+
| ExT([], InferT(z)), zs2 ->
513+
(match t1 with
514+
| WrapT(s2) ->
515+
resolve_always z t1;
516+
[], t1, s2, zs2
517+
| _ ->
518+
error typ.at "could not infer type for unwrap")
505519
| _ -> error typ.at "non-wrapped type for unwrap" in
506-
let t1, zs1, ex = elab_instvar env var in
507520
let s1 =
508521
match t1 with
509522
| WrapT(s1) -> s1
@@ -519,9 +532,16 @@ Trace.debug (lazy ("[UnwrapE] s2 = " ^ string_of_norm_extyp s2));
519532
IL.AppE(f, IL.DotE(ex, "wrap"))
520533

521534
| EL.UnrollE(var, typ) ->
522-
let s, zs1 = elab_typ env typ l in
523-
let rec_t, unroll_t, roll_t, ak = rec_from_extyp typ "unrolling" s in
524535
let var_t = lookup_var env var in
536+
let s, zs1 =
537+
match elab_typ env typ l with
538+
| ExT([], InferT(z)), zs1 ->
539+
let s = ExT([], var_t) in
540+
resolve_always z var_t;
541+
s, zs1
542+
| s, zs1 ->
543+
s, zs1 in
544+
let rec_t, unroll_t, roll_t, ak = rec_from_extyp typ "unrolling" s in
525545
let _, zs2, f = try sub_typ env var_t roll_t [] with Sub e ->
526546
error var.at ("unrolled value does not match annotation:"
527547
^ " " ^ Types.string_of_typ var_t ^ " "

paper.1ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ type OPT = {
266266
}
267267
Opt :> OPT = {
268268
type opt a = wrap (b : type) -> b -> (a ~> b) ~> b
269-
none 'a :# opt a = fun (b : type) (n : b) (s : a ~> b) => n
270-
some 'a x :# opt a = fun (b : type) (n : b) (s : a ~> b) => s x
269+
none 'a :# _ = fun (b : type) (n : b) (s : a ~> b) => n
270+
some 'a x :# _ = fun (b : type) (n : b) (s : a ~> b) => s x
271271
caseopt 'a 'b (xo :# opt a) = xo b
272272
}

prelude.1ml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,8 @@ type OPT = {
112112
}
113113
Opt :> OPT = {
114114
type opt a = wrap (b : type) -> (() ~> b) -> (a ~> b) ~> b
115-
none 'a :# opt a = fun (b : type) (n : () ~> b) (s : a ~> b) => n ()
116-
some 'a x :# opt a = fun (b : type) (n : () ~> b) (s : a ~> b) => s x
115+
none 'a :# _ = fun (b : type) (n : () ~> b) (s : a ~> b) => n ()
116+
some 'a x :# _ = fun (b : type) (n : () ~> b) (s : a ~> b) => s x
117117
caseopt (xo :# opt _) = xo _
118118
}
119119
...Opt
@@ -129,9 +129,9 @@ type ALT = {
129129
}
130130
Alt :> ALT = {
131131
type alt a b = wrap (c : type) -> (a ~> c) -> (b ~> c) ~> c
132-
left 'a 'b x :# alt a b =
132+
left 'a 'b x :# _ =
133133
fun (c : type) (l : a ~> c) (r : b ~> c) => l x
134-
right 'a 'b x :# alt a b =
134+
right 'a 'b x :# _ =
135135
fun (c : type) (l : a ~> c) (r : b ~> c) => r x
136136
casealt (xy :# alt _ _) = xy _
137137
}
@@ -163,8 +163,8 @@ type LIST = {
163163
List :> LIST = {
164164
...{
165165
type list a = wrap (b : type) -> b -> (a ~> b ~> b) ~> b
166-
nil 'a :# list _ = fun (b : type) (n : b) (c : a ~> b ~> b) => n
167-
cons x (xs :# list _) :# list _ =
166+
nil 'a :# _ = fun (b : type) (n : b) (c : a ~> b ~> b) => n
167+
cons x (xs :# list _) :# _ =
168168
fun (b : type) (n : b) (c : _ ~> b ~> b) =>
169169
c x (xs b n c)
170170
foldr (xs :# list _) = xs _

prelude/index.1ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ Alt :> {
3333
case 'a 'b 'o: {inl: a ~> o, inr: b ~> o} -> t a b ~> o
3434
} = {
3535
type t a b = wrap 'o -> {inl: a ~> o, inr: b ~> o} ~> o
36-
inl x :# t _ _ = fun c => c.inl x
37-
inr x :# t _ _ = fun c => c.inr x
36+
inl x :# _ = fun c => c.inl x
37+
inr x :# _ = fun c => c.inr x
3838
case c (x :# t _ _) = x c
3939
}
4040

readme.1ml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ p = f (fun x => x)
4848

4949
type stream = rec t => {hd : int, tl : () ~> opt t} ;; creates rec type
5050
single x :@ stream = {hd = x, tl = fun () => none} ;; b :@ t rolls value into t
51-
({hd = n} :@ stream) = single 5 ;; p :@ t pattern matches on rec value
52-
do Int.print n ;; or:
53-
do Int.print (single 7 @: stream).hd ;; e @: t unrolls rec value directly
51+
({hd = n} :@ _) = single 5 ;; p :@ t pattern matches on rec value
52+
do Int.print n ;; or:
53+
do Int.print (single 7 @: _).hd ;; e @: t unrolls rec value directly
5454

5555

5656

@@ -80,7 +80,7 @@ Opt :> OPT = {
8080
;; Church encoding; it requires the abstract type opt a to be implemented
8181
;; with a polymorphic (i.e., large) type. Thus, wrap the type.
8282
type opt a = wrap (b : type) -> b -> (a ~> b) ~> b
83-
none :# opt _ = fun (b : type) (n : b) (s : _ ~> b) => n
84-
some x :# opt _ = fun (b : type) (n : b) (s : _ ~> b) => s x
83+
none :# _ = fun (b : type) (n : b) (s : _ ~> b) => n
84+
some x :# _ = fun (b : type) (n : b) (s : _ ~> b) => s x
8585
caseopt (xo :# opt _) = xo _
8686
}

regression.1ml

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -44,17 +44,14 @@ Equivalence: {
4444
to 'a 'b: t a b -> a ~> b
4545
from 'a 'b: t a b -> b ~> a
4646
} = {
47-
type T a b = (type p _) ~> p a ~> p b
48-
type t a b = wrap T a b
49-
wr (eq: T _ _) :# t _ _ = eq
50-
un (eq :# t _ _) = eq
51-
transitivity 'a 'b 'c (ab: t a b) (bc: t b c) =
52-
wr fun (type p _) => un bc p << un ab p
53-
reflexivity = wr fun (type p _) => id
54-
to eq a = un eq (type fun x => x) a
55-
from 'a 'b (eq: t a b) b = un eq (type fun b => b ~> a) id b
56-
symmetry 'a 'b (eq: t a b) : t b a =
57-
wr fun (type p _) => un eq (type fun b => p b ~> p a) id
47+
type t a b = wrap (type p _) ~> p a ~> p b
48+
transitivity (ab:# t _ _) (bc:# t _ _) :# _ =
49+
fun (type p _) => bc p << ab p
50+
reflexivity:# _ = fun (type p _) => id
51+
to (eq:# t _ _) a = eq (type fun x => x) a
52+
from (eq:# t _ _) b = eq (type fun b => b ~> _) id b
53+
symmetry (eq:# t _ _) :# _ =
54+
fun (type p _) => eq (type fun b => p b ~> p _) id
5855
}
5956

6057
;;
@@ -173,6 +170,12 @@ Both = {
173170
({x, y = _} as {x = _, y}) = {x = 1, y = 2}
174171
}
175172

173+
type_error {
174+
cannot_infer_unwrap t = t #: _
175+
}
176+
177+
type_error {cannot_infer_unroll t = t @: _}
178+
176179
;;
177180

178181
type_error { type_error 101 }
@@ -301,17 +304,13 @@ ListN = let
301304
nil : p N.Z
302305
(::) 'n : x ~> t x n ~> p (N.S n)
303306
}
304-
type T x n (type t _ _) = (type p _) -> I x p t ~> p n
305307
in {
306-
...rec {type t _ _} => {type t x n = wrap T x n t}
308+
...rec {type t _ _} => {type t x n = wrap (type p _) -> I x p t ~> p n}
307309

308-
case 'x 'n (type p _) (cs: I x p t) (e :# wrap T x n t :@ t x n) =
309-
e p cs
310+
case (type p _) (cs: I _ p t) (e :# _ :@ t _ _) = e p cs
310311

311-
let mk 'x 'n (c: T x n t) = c :# wrap T x n t :@ t x n in {
312-
nil 'x = mk fun (type p _) (r: I x p t) => r.nil
313-
'x (v: x) :: 'n (vs: t x n) = mk fun (type p _) (r: I x p t) => r.:: v vs
314-
}
312+
nil :# _ :@ t _ _ = fun (type p _) (r: I _ p t) => r.nil
313+
v :: vs :# _ :@ t _ _ = fun (type p _) (r: I _ p t) => r.:: v vs
315314
} :> {
316315
type t _ _
317316

russo.1ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,13 +82,13 @@ sift (S : STREAM) :> STREAM = {
8282

8383
Sieve :> STREAM = {
8484
type state = wrap STREAM
85-
start :# state = {
85+
start :# _ = {
8686
type state = int
8787
start = 2
8888
next n = n + 1
8989
value s = s
9090
}
91-
next (S :# state) :# state = sift S
91+
next (S :# state) :# _ = sift S
9292
value (S :# state) = S.value S.start
9393
}
9494

talk.1ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ newpoint = rec (newpoint : int -> int -> point) => fun x y :@ point => {
116116
}
117117

118118
p = newpoint 3 4
119-
p' @: point = (p @: point).move 1 2
119+
p' @: _ = (p @: _).move 1 2
120120
do Int.print (p'.getX ())
121121
do print " "
122122
do Int.print (p'.getY ())

test.1ml

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,8 @@ type OPT = {
8383
}
8484
Opt :> OPT = {
8585
type opt a = wrap (b : type) -> b -> (a ~> b) ~> b
86-
none :# opt _ = fun (b : type) (n : b) (s : _ ~> b) => n
87-
some x :# opt _ = fun (b : type) (n : b) (s : _ ~> b) => s x
86+
none :# _ = fun (b : type) (n : b) (s : _ ~> b) => n
87+
some x :# _ = fun (b : type) (n : b) (s : _ ~> b) => s x
8888
caseopt (xo :# opt _) = xo _
8989
}
9090
...Opt
@@ -99,8 +99,8 @@ type ALT = {
9999
}
100100
Alt :> ALT = {
101101
type alt a b = wrap (c : type) -> (a ~> c) -> (b ~> c) ~> c
102-
left x :# alt _ _ = fun (c : type) (l : _ ~> c) (r : _ ~> c) => l x
103-
right x :# alt _ _ = fun (c : type) (l : _ ~> c) (r : _ ~> c) => r x
102+
left x :# _ = fun (c : type) (l : _ ~> c) (r : _ ~> c) => l x
103+
right x :# _ = fun (c : type) (l : _ ~> c) (r : _ ~> c) => r x
104104
casealt (xy :# alt _ _) = xy _
105105
}
106106
...Alt
@@ -130,8 +130,8 @@ type LIST = {
130130
List :> LIST = {
131131
...{
132132
type list a = wrap (b : type) -> b -> (a ~> b ~> b) ~> b
133-
nil :# list _ = fun (b : type) (n : b) (c : _ ~> b ~> b) => n
134-
cons x (xs :# list _) :# list _ =
133+
nil :# _ = fun (b : type) (n : b) (c : _ ~> b ~> b) => n
134+
cons x (xs :# list _) :# _ =
135135
fun (b : type) (n : b) (c : _ ~> b ~> b) =>
136136
c x (xs b n c)
137137
foldr (xs :# list _) = xs _
@@ -277,10 +277,10 @@ in {
277277
type int = primitive "int"
278278
type t = rec a => int
279279
v = 3 :@ t
280-
n = v @: t
281-
(m :@ t) = v
280+
n = v @: _
281+
(m :@ _) = v
282282
type u a = rec _ => a
283283
w = 4 :@ u int
284-
(x :@ u int) = w
285-
((x :@ t, y) :@ u (_, _)) = (6 :@ t, 7) :@ u (_, _)
286-
((x' :@ t, y') :@ u _) = (6 :@ t, 7) :@ u _
284+
(x :@ _) = w
285+
((x :@ _, y) :@ _) = (6 :@ t, 7) :@ u _
286+
((x' :@ _, y') :@ _) = (6 :@ t, 7) :@ u _

0 commit comments

Comments
 (0)