Go to the documentation of this file. 1 package com.cliffc.aa.HM;
5 import org.junit.Before;
10 import static org.junit.Assert.assertEquals;
16 private void run( String prog, String rez_hm,
Type rez_gcp ) {
19 assertEquals(rez_hm,syn.
_hmt.
p());
33 private static final String[]
XY =
new String[]{
"^",
"x",
"y"};
34 private static final String[]
N1V1 =
new String[]{
"^",
"n1",
"v1"};
37 @Test(expected = RuntimeException.class)
46 @Test
public void test03() {
run(
"{ z -> (pair (z 0) (z \"abc\")) }" ,
47 "{ { *[0,4]\"abc\"? -> A } -> ( $A, $A)[7] }",
tfs(
tuple2)); }
49 @Test
public void test04() {
run(
"fact = { n -> (if (?0 n) 1 (* n (fact (dec n))))}; fact",
55 Root syn =
HM9.
hm(
"({ x -> (pair (x 3) (x 5)) } {y->y})");
57 assertEquals(
"( nint8, nint8)[7]",syn.
_hmt.
p());
66 Root syn =
HM9.
hm(
"id={x->x}; (pair (id 3) (id \"abc\"))");
68 assertEquals(
"( 3, *[4]\"abc\")[7]",syn.
_hmt.
p());
82 @Test
public void test08() {
run(
"g = {f -> 5}; (g g)",
86 @Test
public void test09() {
run(
"{ g -> f = { x -> g }; (pair (f 3) (f \"abc\"))}",
89 @Test
public void test10() {
run(
"{ f g -> (f g)}",
93 @Test
public void test11() {
run(
"{ f g -> { arg -> (g (f arg))} }",
97 @Test
public void test12() {
run(
"map = { fun -> { x -> 2 } }; ((map 3) 5)",
101 @Test
public void test13() {
run(
"map = { fun -> { x -> (fun x)}}; { p -> 5 }",
108 Root syn =
HM9.
hm(
"map = { fun -> { x -> (fun x)}};"+
109 "(pair ((map str) 5) ((map factor) 2.3))");
111 assertEquals(
"( *[4]str, flt64)[7]",syn.
_hmt.
p());
120 @Test
public void test15() {
run(
"map = { fun x -> (fun x)}; (map {a->3} 5)",
124 @Test
public void test16() {
run(
"map = { fun x -> (fun x)}; (map { a-> (pair a a)} 5)",
127 @Test
public void test17() {
run(
"fcn = { p -> { a -> (pair a a) }};"+
128 "map = { fun x -> (fun x)};"+
129 "{ q -> (map (fcn q) 5)}",
143 Root syn =
HM9.
hm(
"fcn = {p -> (if p {a -> (pair a a)} {b -> (pair b (pair 3 b))})};"+
144 "map = { fun x -> (fun x)};"+
145 "{ q -> (map (fcn q) 5)}");
147 assertEquals(
"{ A -> ( B:Cannot unify A:( 3, $A)[7] and 5, $B)[7] }",syn.
_hmt.
p());
155 @Test
public void test19() {
run(
"cons ={x y-> {cadr -> (cadr x y)}};"+
156 "cdr ={mycons -> (mycons { p q -> q})};"+
168 Root syn =
HM9.
hm(
"cons ={x y-> {cadr -> (cadr x y)}};"+
169 "cdr ={mycons -> (mycons { p q -> q})};"+
170 "map ={fun parg -> (fun (cdr parg))};"+
171 "(pair (map str (cons 0 5)) (map isempty (cons 0 \"abc\")))");
173 assertEquals(
"( *[4]str, int1)[7]",syn.
_hmt.
p());
182 @Test
public void test21() {
run(
"f0 = { f x -> (if (?0 x) 1 (f (f0 f (dec x)) 2))}; (f0 * 99)",
188 @Test
public void test22() {
run(
"f0 = { f x -> (if (?0 x) 1 (* (f0 f (dec x)) 2))}; (f0 f0 99)",
193 " is_odd = { n -> (if (?0 n) 0 (is_even (dec n)))}; "+
194 " { n -> (if (?0 n) 1 (is_odd (dec n)))};"+
200 " cons = {x y -> {cadr -> (cadr x y)}};"+
201 " cdr = {mycons -> (mycons { p q -> q})};"+
202 " (cdr (cons 2 { z -> (g z) }));"+
203 " (pair (fgz 3) (fgz 5))"+
206 "{ { nint8 -> A } -> ( $A, $A)[7] }",
tfs(
tuple2)); }
210 "@{ x = 2, y = 3}[9]",
224 "Missing field x in @{ y = 3}[9]",
228 "{ A -> @{ x = $A, y = $A}[9] }",
tfs(
tuple9)); }
231 @Test
public void test30() {
run(
"{ pred -> .x (if pred @{x=2,y=3} @{x=3,z= \"abc\"}) }",
236 @Test
public void test31() {
run(
"{ sq -> (* .x sq .y sq) }",
242 if( nil ) aliases = aliases.
meet_nil();
257 Root syn =
HM9.
hm(
"map = { fcn lst -> @{ n1 = (map fcn .n0 lst), v1 = (fcn .v0 lst) } }; map");
259 assertEquals(
"{ { A -> B } C:@{ n0 = $C, v0 = $A}[] -> D:@{ n1 = $D, v1 = $B}[9] }",syn.
_hmt.
p());
269 Root syn =
HM9.
hm(
"map = { fcn lst -> (if lst @{ n1=(map fcn .n0 lst), v1=(fcn .v0 lst) } 0) }; map");
271 assertEquals(
"{ { A -> B } C:@{ n0 = $C, v0 = $A}[0] -> D:@{ n1 = $D, v1 = $B}[0,9] }",syn.
_hmt.
p());
279 Root syn =
HM9.
hm(
"map = { fcn lst -> (if lst @{ n1 = (map fcn .n0 lst), v1 = (fcn .v0 lst) } 0) }; (map dec @{n0 = 0, v0 = 5})");
281 assertEquals(
"A:@{ n1 = $A, v1 = int64}[0,9]",syn.
_hmt.
p());
293 run(
"p0 = { x y z -> (triple x y z) };"+
294 "p1 = (triple p0 p0 p0);"+
295 "p2 = (triple p1 p1 p1);"+
296 "p3 = (triple p2 p2 p2);"+
298 "( ( ( { A B C -> ( $A, $B, $C)[8] }, { D E F -> ( $D, $E, $F)[8] }, { G H I -> ( $G, $H, $I)[8] })[8], ( { J K L -> ( $J, $K, $L)[8] }, { M N O -> ( $M, $N, $O)[8] }, { P Q R -> ( $P, $Q, $R)[8] })[8], ( { S T U -> ( $S, $T, $U)[8] }, { V21 V22 V23 -> ( $V21, $V22, $V23)[8] }, { V24 V25 V26 -> ( $V24, $V25, $V26)[8] })[8])[8], ( ( { V27 V28 V29 -> ( $V27, $V28, $V29)[8] }, { V30 V31 V32 -> ( $V30, $V31, $V32)[8] }, { V33 V34 V35 -> ( $V33, $V34, $V35)[8] })[8], ( { V36 V37 V38 -> ( $V36, $V37, $V38)[8] }, { V39 V40 V41 -> ( $V39, $V40, $V41)[8] }, { V42 V43 V44 -> ( $V42, $V43, $V44)[8] })[8], ( { V45 V46 V47 -> ( $V45, $V46, $V47)[8] }, { V48 V49 V50 -> ( $V48, $V49, $V50)[8] }, { V51 V52 V53 -> ( $V51, $V52, $V53)[8] })[8])[8], ( ( { V54 V55 V56 -> ( $V54, $V55, $V56)[8] }, { V57 V58 V59 -> ( $V57, $V58, $V59)[8] }, { V60 V61 V62 -> ( $V60, $V61, $V62)[8] })[8], ( { V63 V64 V65 -> ( $V63, $V64, $V65)[8] }, { V66 V67 V68 -> ( $V66, $V67, $V68)[8] }, { V69 V70 V71 -> ( $V69, $V70, $V71)[8] })[8], ( { V72 V73 V74 -> ( $V72, $V73, $V74)[8] }, { V75 V76 V77 -> ( $V75, $V76, $V77)[8] }, { V78 V79 V80 -> ( $V78, $V79, $V80)[8] })[8])[8])[8]",
305 Root syn =
HM9.
hm(
"map = { lst -> (if lst @{ n1= arg= .n0 lst; (if arg @{ n1=(map .n0 arg), v1=(str .v0 arg)} 0), v1=(str .v0 lst) } 0) }; map");
307 assertEquals(
"{ A:@{ n0 = @{ n0 = $A, v0 = int64}[0], v0 = int64}[0] -> B:@{ n1 = @{ n1 = $B, v1 = *[4]str}[0,9], v1 = *[4]str}[0,10] }",syn.
_hmt.
p());
319 cycle_strX = cycle_str3;
326 cycle_strX = cycle_str2;
334 @Test
public void test37() {
run(
"x = { y -> (x (y y))}; x",
339 @Test
public void test38() {
run(
"{ x -> y = ( x .v x ); 0}",
340 "{ Cannot unify @{ v = A}[] and { A -> B } -> 0 }",
tfs(
Type.
XNIL)); }
345 Root syn =
HM9.
hm(
"x = { z -> z}; (x { y -> .u y})");
347 assertEquals(
"{ @{ u = A}[] -> $A }",syn.
_hmt.
p());
357 Root syn =
HM9.
hm(
"x = w = (x x); { z -> z}; (x { y -> .u y})");
359 assertEquals(
"Cannot unify A:{ $A -> $A } and @{ u = A}[]",syn.
_hmt.
p());
377 Root syn =
HM9.
hm(
"map={lst fcn -> (fcn .y lst) }; "+
378 "in_int=@{ x=0 y=2}; " +
379 "in_str=@{ x=0 y=\"abc\"}; " +
380 "out_str = (map in_int str); " +
381 "out_bool= (map in_str { xstr -> (eq xstr \"def\")}); "+
382 "(pair out_str out_bool)");
384 assertEquals(
"( *[4]str, int1)[7]",syn.
_hmt.
p());
394 Root syn =
HM9.
hm(
"pred = 0; s1 = @{ x=\"abc\" }; s2 = @{ y=3.4 }; .y (if pred s1 s2)");
397 assertEquals(
"3.4000000953674316",syn.
_hmt.
p());
399 assertEquals(
"Missing field y in @{ x = *[4]\"abc\"}[9]",syn.
_hmt.
p());
407 Root syn =
HM9.
hm(
"pred = 0; s1 = @{ x=\"abc\" }; s2 = @{ y=3.4 }; z = (if pred s1 s2); .y s2");
409 assertEquals(
"3.4000000953674316",syn.
_hmt.
p());
416 Root syn =
HM9.
hm(
"fun = (if (isempty \"abc\") {x->x} {x->1.2}); (fun @{})");
419 assertEquals(
"1.2000000476837158",syn.
_hmt.
p());
421 assertEquals(
"Cannot unify 1.2000000476837158 and )[9]",syn.
_hmt.
p());
432 "loop = { name cnt ->" +
435 " fltfun = (if name id {x->3});" +
436 " (fltfun \"abc\")" +
442 "(loop \"def\" (id 2))");
446 :
"Cannot unify *[4]\"abc\" and 3",
458 "May be nil when loading field x",
Type.
XSCALAR); }
461 @Test
public void test47() {
run(
"{ pred -> .x (if pred @{x=3} 0)}",
462 "{ A -> May be nil when loading field x }",
tfs(
TypeInt.
con(3))); }
465 @Test
public void test48() {
run(
"{ pred -> tmp=(if pred @{x=3} 0); (if tmp .x tmp 4) }",
472 " map = { fun x -> (fun x) };\n" +
473 " (pair (map {str0 -> .x str0 } @{x = 3} )\n" +
474 " (map {str1 -> (if str1 .x str1 4)} (if pred @{x = 5} 0))\n" +
478 assertEquals(
"{ A -> ( 3, nint8)[7] }",syn.
_hmt.
p());
487 " map = { fun x -> (fun x) };\n" +
488 " (pair (map {str0 -> .x str0 } @{x = 3} )\n" +
489 " (map {str1 -> .x str1 } (if pred @{x = 5} 0))\n" +
493 assertEquals(
"{ A -> May be nil when loading field x }",syn.
_hmt.
p());
static final TypeMemPtr tuple55
static TypeTuple make_args(Type[] ts)
static TypeTuple make_ret(Type trez)
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)
static TypeMemPtr build_cycle(int alias, boolean nil, Type fld)
A memory-based collection of optionally named fields.
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 TypeFunSig tfs(Type ret)
static final TypeMemPtr tuple82
static final TypeMemPtr tuple2
static final TypeInt NINT8
static TypeFunPtr make(BitsFun fidxs, int nargs, Type disp)
static TypeFld[] flds(Type t1)
static Root hm(String sprog)
static TypeFld malloc(String fld, Type t, Access access, int order)
TypeStruct approx(int cutoff, int alias)
static final TypeMemPtr tuplen2
static BitsAlias make0(int bit)
static final boolean DO_GCP
static final String[] N1V1
static TypeStruct make(String fld_name, Type t)
static final TypeMemPtr STRPTR
void run(String prog, String rez_hm, Type rez_gcp)
static final TypeInt BOOL
static final TypeFlt FLT64
static final TypeFunSig ret_tuple2
static TypeMemPtr make(BitsAlias aliases, TypeObj obj)