Go to the documentation of this file. 1 package com.cliffc.aa.HM;
5 import org.junit.Before;
8 import static org.junit.Assert.assertEquals;
14 private void run( String prog, String rez_hm,
Type rez_gcp ) {
17 assertEquals(rez_hm,syn.
_hmt.
p());
35 @Test(expected = RuntimeException.class)
44 @Test
public void test03() {
run(
"{ z -> (pair (z 0) (z \"abc\")) }" ,
45 "{ { *[0,4]\"abc\"? -> A } -> ( A, A) }",
tfs(
tuple2)); }
47 @Test
public void test04() {
run(
"fact = { n -> (if (eq0 n) 1 (* n (fact (dec n))))}; fact",
53 Root syn =
HM.
hm(
"({ x -> (pair (x 3) (x 5)) } {y->y})");
55 assertEquals(
"( nint8, nint8)",syn.
_hmt.
p());
64 Root syn =
HM.
hm(
"id={x->x}; (pair (id 3) (id \"abc\"))");
66 assertEquals(
"( 3, *[4]\"abc\")",syn.
_hmt.
p());
80 @Test
public void test08() {
run(
"g = {f -> 5}; (g g)",
84 @Test
public void test09() {
run(
"{ g -> f = { x -> g }; (pair (f 3) (f \"abc\"))}",
87 @Test
public void test10() {
run(
"{ f g -> (f g)}",
91 @Test
public void test11() {
run(
"{ f g -> { arg -> (g (f arg))} }",
95 @Test
public void test12() {
run(
"map = { fun -> { x -> 2 } }; ((map 3) 5)",
99 @Test
public void test13() {
run(
"map = { fun -> { x -> (fun x)}}; { p -> 5 }",
106 Root syn =
HM.
hm(
"map = { fun -> { x -> (fun x)}};"+
107 "(pair ((map str) 5) ((map factor) 2.3))");
109 assertEquals(
"( *[4]str, flt64)",syn.
_hmt.
p());
118 @Test
public void test15() {
run(
"map = { fun x -> (fun x)}; (map {a->3} 5)",
122 @Test
public void test16() {
run(
"map = { fun x -> (fun x)}; (map { a-> (pair a a)} 5)",
125 @Test
public void test17() {
run(
"fcn = { p -> { a -> (pair a a) }};"+
126 "map = { fun x -> (fun x)};"+
127 "{ q -> (map (fcn q) 5)}",
141 Root syn =
HM.
hm(
"fcn = {p -> (if p {a -> (pair a a)} {b -> (pair b (pair 3 b))})};"+
142 "map = { fun x -> (fun x)};"+
143 "{ q -> (map (fcn q) 5)}");
145 assertEquals(
"{ A? -> ( B:Cannot unify A:( 3, A) and 5, B) }",syn.
_hmt.
p());
153 @Test
public void test19() {
run(
"cons ={x y-> {cadr -> (cadr x y)}};"+
154 "cdr ={mycons -> (mycons { p q -> q})};"+
166 Root syn =
HM.
hm(
"cons ={x y-> {cadr -> (cadr x y)}};"+
167 "cdr ={mycons -> (mycons { p q -> q})};"+
168 "map ={fun parg -> (fun (cdr parg))};"+
169 "(pair (map str (cons 0 5)) (map isempty (cons 0 \"abc\")))");
171 assertEquals(
"( *[4]str, int1)",syn.
_hmt.
p());
180 @Test
public void test21() {
run(
"f0 = { f x -> (if (eq0 x) 1 (f (f0 f (dec x)) 2))}; (f0 * 99)",
186 @Test
public void test22() {
run(
"f0 = { f x -> (if (eq0 x) 1 (* (f0 f (dec x)) 2))}; (f0 f0 99)",
191 " is_odd = { n -> (if (eq0 n) 0 (is_even (dec n)))}; "+
192 " { n -> (if (eq0 n) 1 (is_odd (dec n)))};"+
198 " cons = {x y -> {cadr -> (cadr x y)}};"+
199 " cdr = {mycons -> (mycons { p q -> q})};"+
200 " (cdr (cons 2 { z -> (g z) }));"+
201 " (pair (fgz 3) (fgz 5))"+
204 "{ { nint8 -> A } -> ( A, A) }",
tfs(
tuple2)); }
222 "Missing field x in @{ y = 3}",
226 "{ A -> @{ x = A, y = A} }",
tfs(
tuple9)); }
229 @Test
public void test30() {
run(
"{ pred -> (if pred @{x=2,y=3} @{x=3,z= \"abc\"}) .x }",
234 @Test
public void test31() {
run(
"{ sq -> (* sq.x sq.y) }",
240 if( nil ) aliases = aliases.
meet_nil();
254 if( nil ) aliases0 = aliases0.
meet_nil();
255 if( nil ) aliases9 = aliases9.
meet_nil();
272 Root syn =
HM.
hm(
"map = { fcn lst -> @{ n1 = (map fcn lst.n0), v1 = (fcn lst.v0) } }; map");
274 assertEquals(
"{ { A -> B } C:@{ n0 = C, v0 = A} -> D:@{ n1 = D, v1 = B} }",syn.
_hmt.
p());
284 Root syn =
HM.
hm(
"map = { fcn lst -> (if lst @{ n1=(map fcn lst.n0), v1=(fcn lst.v0) } 0) }; map");
286 assertEquals(
"{ { A -> B } C:@{ n0 = C, v0 = A}? -> D:@{ n1 = D, v1 = B}? }",syn.
_hmt.
p());
294 Root syn =
HM.
hm(
"map = { fcn lst -> (if lst @{ n1 = (map fcn lst.n0), v1 = (fcn lst.v0) } 0) }; (map dec @{n0 = 0, v0 = 5})");
296 assertEquals(
"A:@{ n1 = A, v1 = int64}?",syn.
_hmt.
p());
308 run(
"p0 = { x y z -> (triple x y z) };"+
309 "p1 = (triple p0 p0 p0);"+
310 "p2 = (triple p1 p1 p1);"+
311 "p3 = (triple p2 p2 p2);"+
313 "( ( ( { A B C -> ( A, B, C) }, { D E F -> ( D, E, F) }, { G H I -> ( G, H, I) }), ( { J K L -> ( J, K, L) }, { M N O -> ( M, N, O) }, { P Q R -> ( P, Q, R) }), ( { S T U -> ( S, T, U) }, { V21 V22 V23 -> ( V21, V22, V23) }, { V24 V25 V26 -> ( V24, V25, V26) })), ( ( { V27 V28 V29 -> ( V27, V28, V29) }, { V30 V31 V32 -> ( V30, V31, V32) }, { V33 V34 V35 -> ( V33, V34, V35) }), ( { V36 V37 V38 -> ( V36, V37, V38) }, { V39 V40 V41 -> ( V39, V40, V41) }, { V42 V43 V44 -> ( V42, V43, V44) }), ( { V45 V46 V47 -> ( V45, V46, V47) }, { V48 V49 V50 -> ( V48, V49, V50) }, { V51 V52 V53 -> ( V51, V52, V53) })), ( ( { V54 V55 V56 -> ( V54, V55, V56) }, { V57 V58 V59 -> ( V57, V58, V59) }, { V60 V61 V62 -> ( V60, V61, V62) }), ( { V63 V64 V65 -> ( V63, V64, V65) }, { V66 V67 V68 -> ( V66, V67, V68) }, { V69 V70 V71 -> ( V69, V70, V71) }), ( { V72 V73 V74 -> ( V72, V73, V74) }, { V75 V76 V77 -> ( V75, V76, V77) }, { V78 V79 V80 -> ( V78, V79, V80) })))",
320 Root syn =
HM.
hm(
"map = { lst -> (if lst @{ n1= arg= lst.n0; (if arg @{ n1=(map arg.n0), v1=(str arg.v0)} 0), v1=(str lst.v0) } 0) }; map");
322 assertEquals(
"{ A:@{ n0 = @{ n0 = A, v0 = int64}?, v0 = int64}? -> B:@{ n1 = @{ n1 = B, v1 = *[4]str}?, v1 = *[4]str}? }",syn.
_hmt.
p());
327 @Test
public void test37() {
run(
"x = { y -> (x (y y))}; x",
332 @Test
public void test38() {
run(
"{ x -> y = ( x x.v ); 0}",
333 "{ Cannot unify @{ v = A} and { A -> B } -> A? }",
tfs(
Type.
XNIL)); }
338 Root syn =
HM.
hm(
"x = { z -> z}; (x { y -> y.u})");
340 assertEquals(
"{ @{ u = A} -> A }",syn.
_hmt.
p());
350 Root syn =
HM.
hm(
"x = w = (x x); { z -> z}; (x { y -> y.u})");
352 assertEquals(
"Cannot unify A:{ A -> A } and @{ u = A}",syn.
_hmt.
p());
370 Root syn =
HM.
hm(
"map={lst fcn -> (fcn lst.y) }; "+
371 "in_int=@{ x=0 y=2}; " +
372 "in_str=@{ x=0 y=\"abc\"}; " +
373 "out_str = (map in_int str); " +
374 "out_bool= (map in_str { xstr -> (eq xstr \"def\")}); "+
375 "(pair out_str out_bool)");
377 assertEquals(
"( *[4]str, int1)",syn.
_hmt.
p());
387 Root syn =
HM.
hm(
"pred = 0; s1 = @{ x=\"abc\" }; s2 = @{ y=3.4 }; (if pred s1 s2).y");
390 assertEquals(
"3.4000000953674316",syn.
_hmt.
p());
392 assertEquals(
"Missing field y in @{ x = *[4]\"abc\"}",syn.
_hmt.
p());
400 Root syn =
HM.
hm(
"pred = 0; s1 = @{ x=\"abc\" }; s2 = @{ y=3.4 }; z = (if pred s1 s2); s2.y");
402 assertEquals(
"3.4000000953674316",syn.
_hmt.
p());
409 Root syn =
HM.
hm(
"fun = (if (isempty \"abc\") {x->x} {x->1.2}); (fun @{})");
412 assertEquals(
"1.2000000476837158",syn.
_hmt.
p());
414 assertEquals(
"Cannot unify 1.2000000476837158 and ()",syn.
_hmt.
p());
425 "loop = { name cnt ->" +
428 " fltfun = (if name id {x->3});" +
429 " (fltfun \"abc\")" +
435 "(loop \"def\" (id 2))");
439 :
"Cannot unify *[4]\"abc\" and 3",
451 "May be nil when loading field x",
Type.
XSCALAR); }
454 @Test
public void test47() {
run(
"{ pred -> (if pred @{x=3} 0).x}",
455 "{ A? -> May be nil when loading field x }",
tfs(
TypeInt.
con(3))); }
458 @Test
public void test48() {
run(
"{ pred -> tmp=(if pred @{x=3} 0); (if tmp tmp.x 4) }",
465 " map = { fun x -> (fun x) };\n" +
466 " (pair (map {str0 -> str0.x } @{x = 3} )\n" +
467 " (map {str1 -> (if str1 str1.x 4)} (if pred @{x = 5} 0))\n" +
471 assertEquals(
"{ A? -> ( 3, nint8) }",syn.
_hmt.
p());
480 " map = { fun x -> (fun x) };\n" +
481 " (pair (map {str0 -> str0.x } @{x = 3} )\n" +
482 " (map {str1 -> str1.x } (if pred @{x = 5} 0))\n" +
486 assertEquals(
"{ A? -> May be nil when loading field x }",syn.
_hmt.
p());
493 Root syn =
HM.
hm(
"total_size = { a as ->" +
496 " (total_size as.val as.next))" +
503 assertEquals(
"{ A:@{ size = int64} B:@{ next = B, val = A}? -> int64 }",syn.
_hmt.
p());
515 " thenElse = {then else->(then void) }"+
518 " and= {b -> false}"+
520 " thenElse = {then else->(else void) }"+
522 "forceSubtyping ={b ->(if b true false)};"+
524 "bool=@{true=(forceSubtyping 1), false=(forceSubtyping 0), force=forceSubtyping};"+
526 "a=(bool.false.thenElse { x-> 3 } { y-> 4 });"+
527 "b=(bool.false.thenElse { z->@{}} { z->@{}});"+
529 "@{a=a,b=b, bool=bool}"+
543 assertEquals(
"@{ a = nint8, b = (), bool = @{ false = A:@{ and = { A -> A }, or = { A -> A }, thenElse = { { () -> B } { () -> B } -> B }}, force = { C? -> D:@{ and = { D -> D }, or = { D -> D }, thenElse = { { () -> E } { () -> E } -> E }} }, true = F:@{ and = { F -> F }, or = { F -> F }, thenElse = { { () -> G } { () -> G } -> G }}}}",syn.
_hmt.
p());
565 @Test
public void test53() {
run(
"{ x y -> (if x x y) }",
569 @Test
public void test54() {
run(
"{ x y -> (if x (if x x y) y) }",
577 " or = {b -> true} "+
578 " not = {unused ->true} "+
579 " thenElse = {then else->(then void) }"+
582 " and = {b -> false} "+
584 " not = {unused ->true} "+
585 " thenElse = {then else->(else void) }"+
587 "boolSub ={b ->(if b true false)}; "+
588 "@{true=(boolSub 1) false=(boolSub 0)} "+
591 assertEquals(
"@{ false = A:@{ and = { A -> A }, "+
592 "not = { B -> A }, "+
594 "thenElse = { { () -> C } { () -> C } -> C }"+
596 "true = D:@{ and = { D -> D }, "+
597 "not = { E -> D }, "+
599 "thenElse = { { () -> F } { () -> F } -> F }"+
626 " rite = @{n1 = left v1 = 7 }; "+
627 " @{ n1 = rite v1 = 7 };"+
631 assertEquals(
"A:@{ n1 = @{ n1 = A, v1 = 7}, v1 = 7}",syn.
_hmt.
p());
640 " not = {unused -> all.false}, "+
641 " thenElse = {then else->(then 7) } "+
644 " not = {unused -> all.true}, "+
645 " thenElse = {then else->(else 7) } "+
647 "boolSub ={b ->(if b true false)}; "+
648 "@{true=true, false=false, boolSub=boolSub};"+
652 assertEquals(
"@{ boolSub = { A? -> @{ not = { B -> C:@{ not = { D -> C }, thenElse = { { 7 -> E } { 7 -> E } -> E }} }, thenElse = { { 7 -> F } { 7 -> F } -> F }} }, false = C, true = C}",syn.
_hmt.
p());
static final Type NO_DISP
static TypeMemPtr build_cycle(int alias, boolean nil, Type fld)
static final TypeMemPtr tuple2
static TypeTuple make_args(Type[] ts)
static BitsFun make0(int bit)
static TypeTuple make_ret(Type trez)
static TypeFunSig tfs(Type ret)
B make(boolean any, long[] bits)
static final Type XSCALAR
an implementation of language AA
static TypeFunSig make(String[] args, TypeTuple formals, TypeTuple ret)
static final TypeFld NO_DISP
static TypeInt con(long con)
A memory-based collection of optionally named fields.
static TypeFld make(String fld, Type t, int order)
static final boolean DO_HM
static final TypeMemPtr tuple9
static final TypeInt INT64
static Type con(double con)
static final Type XNSCALR
static final TypeObj XOBJ
static final TypeInt NINT8
static TypeFunPtr make(BitsFun fidxs, int nargs, Type disp)
static TypeStruct make_tups(Type t0, Type t1)
static final TypeMemPtr tuplen2
static TypeFld[] flds(Type t1)
static TypeFld[] tups(Type t1, Type t2)
static final TypeMemPtr tuple82
static Root hm(String sprog)
TypeStruct approx(int cutoff, int alias)
static TypeMemPtr build_cycle2(boolean nil, Type fld)
static BitsAlias make0(int bit)
static final boolean DO_GCP
static TypeStruct make(String fld_name, Type t)
static final TypeMemPtr tuple55
static final TypeFunSig ret_tuple2
void run(String prog, String rez_hm, Type rez_gcp)
static final TypeMemPtr STRPTR
static final TypeInt BOOL
static TypeStruct make_tups(Type t0, Type t1, Type t2)
static final TypeFlt FLT64
static TypeMemPtr make(BitsAlias aliases, TypeObj obj)