1193323Sedopen arithmeticTheory HolKernel boolLib Parse testutils
2193323Sed
3193323Sedfun fail() = die "FAILED\n"
4193323Sed
5193323Sedval _ = tprint "Testing that I can parse num$0"
6193323Sedval _ = (Parse.Term`num$0`; OK())
7193323Sed        handle HOL_ERR _ => fail()
8193323Sed
9193323Sedval _ = tprint "Testing that I can't parse num$1"
10193323Sedval _ = (Parse.Term`num$1`; fail()) handle HOL_ERR _ => OK();
11193323Sed
12193323Sed
13193323Sedval _ = tprint "Testing that bool$0 fails"
14193323Sedval _ = (Parse.Term`bool$0`; fail()) handle HOL_ERR _ => OK()
15193323Sed
16193323Sedval _ = tprint "Testing that num$01 fails"
17193323Sedval _ = (Parse.Term`num$01`; fail()) handle HOL_ERR _ => OK()
18193323Sed
19193323Sedval _ = let
20193323Sed  val _ = tprint "Anthony's pattern-overloading bug"
21193323Sed  val b2n_def = new_definition("b2n_def", ``b2n b = if b then 1 else 0``)
22193323Sed  val _ = overload_on ("foo", ``\(x:num#'a),(y:num#'b). b2n (FST x = FST y)``)
23193323Sed  val res = trace ("PP.catch_withpp_err", 0) term_to_string ``foo(x,y)``
24193323Sed            handle Fail _ => ""
25193323Sedin
26193323Sed  if res <> "" then OK() else fail()
27193323Sedend
28193323Sed
29193323Sedval _ = let
30193323Sed  val tpp = let open testutils
31193323Sed            in
32193323Sed              unicode_off (raw_backend testutils.tpp)
33193323Sed            end
34193323Sedin
35193323Sed  List.app tpp [
36193323Sed    "case e1 of 0 => (case e2 of 0 => 1 | SUC n => n + 1) | SUC m => m * 2",
37193323Sed    "case e1 of 0 => 1 | SUC n => case e2 of 0 => 2 | SUC m => 3",
38193323Sed    "(case x of 0 => (\\x. x) | SUC n => (\\m. m + n)) y",
39193323Sed    "!a b c d e f g.\n\
40193323Sed    \    foo' a b /\\ bar a c /\\ baz a c d /\\ qux f g /\\ foo' f g ==>\n\
41193323Sed    \    (a + b * c = 10 * d + e + f + g)"
42193323Sed  ] ;
43193323Sed  trace ("PP.print_firstcasebar", 1) tpp "case e of | 0 => 1 | SUC n => n + 2";
44193323Sed  trace ("PP.print_firstcasebar", 1) tpp
45193323Sed    "case f e n longvarname of\n\
46193323Sed    \| 0 => superlongvarname * secondsuperlongname + 1\n\
47193323Sed    \| SUC n => n + 2"
48193323Sedend
49193323Sed
50193323Sed
51193323Sed
52193323Sed
53193323Sedfun colourpp_test (testname, s, expected) =
54193323Sed    testutils.tpp_expected {testf = (fn _ => testname),
55193323Sed                            input = s,
56193323Sed                            output = expected}
57193323Sed
58193323Sedval _ = Parse.current_backend := PPBackEnd.vt100_terminal
59193323Sed
60193323Sedfun bound s = "\^[[0;32m" ^ s ^ "\^[[0m"
61193323Sedfun free s = "\^[[0;1;34m" ^ s ^ "\^[[0m"
62193323Sedval concat = String.concat
63193323Sed
64193323Sedval colourtests =
65193323Sed    [("&3 should print as 3", "&3", "3"),
66193323Sed     ("Pretty-printing of case expression bound variables",
67193323Sed      "case x of 0 => 1 | SUC n => n * 2",
68193323Sed      concat ["case ",free "x"," of 0 => 1 | ", "SUC ", bound "n", " => ",
69193323Sed              bound "n", " * 2"]),
70193323Sed     ("Pretty-printing of case in rand position",
71193323Sed      "f (case x of 0 => 1 | SUC n => n * 2)",
72193323Sed      concat [free "f", " (case ", free "x", " of 0 => 1 | SUC ", bound "n",
73193323Sed              " => ", bound "n", " * 2)"]),
74193323Sed     ("Colouring simple LET", "let x = 3 in x + 1",
75193323Sed      concat ["let ", bound "x", " = 3 in ", bound "x", " + 1"]),
76193323Sed     ("Colouring simple LET with free on right",
77193323Sed      "let x = y + 2 in x * 2",
78193323Sed      concat ["let ", bound "x", " = ", free "y", " + 2 in ", bound "x",
79193323Sed              " * 2"]),
80193323Sed     ("Colouring LET with ands",
81193323Sed      "let x = 10 and y = x + 6 in x + y",
82193323Sed      concat ["let ", bound "x", " = 10 and ", bound "y", " = ", free "x",
83193323Sed              " + 6 in ", bound "x", " + ", bound "y"]),
84193323Sed     ("Colouring LET binding local function",
85193323Sed      "let f x = x * 2 in x + f 10",
86193323Sed      concat ["let ", bound "f", " ", bound "x", " = ", bound "x", " * 2 in ",
87193323Sed              free "x", " + ", bound "f", " 10"])
88193323Sed    ]
89193323Sed
90193323Sedval _ = app colourpp_test colourtests
91193323Sed
92193323Sedopen groundEval numeralTheory
93193323Sed
94193323Sedval _ = overload_on ("B1", ``BIT1``);
95193323Sedval _ = overload_on ("B2", ``BIT2``);
96193323Sedval _ = overload_on ("iZ", ``numeral$iZ``);
97193323Sedval _ = overload_on ("NUM", ``NUMERAL``)
98193323Sed
99193323Sedval ncset = HOLset.addList(empty_tmset,
100193323Sed                           [``NUMERAL``, ``BIT1``, ``BIT2``,
101193323Sed                            ``0:num``, ``ZERO``]);
102193323Sed
103193323Sedval ge0 = GE {constrs = ncset, rwts = Net.empty, case_consts = empty_tmset }
104193323Sedval ge = List.foldl (fn (th,ge) => add_rwt th ge) ge0
105193323Sed                    (Rewrite.mk_rewrites numeralTheory.numeral_distrib @
106193323Sed                     Rewrite.mk_rewrites numeralTheory.numeral_add @
107193323Sed                     Rewrite.mk_rewrites numeralTheory.numeral_suc @
108193323Sed                     Rewrite.mk_rewrites numeralTheory.numeral_iisuc)
109193323Sed
110193323Sedfun dot t = reduction ge (vTreeOf ge t) t (Conv (fn x => x));
111193323Sedfun testdot t expected = let
112193323Sed  val result = dot t
113193323Sedin
114193323Sed    aconv (result_term (dot t)) expected andalso
115193323Sed    result_tree result = KnownValue
116193323Sed  orelse
117218893Sdim    die ("Reduction of " ^ term_to_string t ^ " didn't give back " ^
118193323Sed         term_to_string expected)
119193323Sedend;
120193323Sed
121193323Sedval _ = testdot ``1 + 1`` ``2``;
122193323Sedval _ = testdot ``2 + 1`` ``3``;
123218893Sdimval _ = testdot ``3 + 4`` ``7``;
124193323Sedval _ = testdot ``4 + 5 + 9`` ``18``;
125218893Sdim
126193323Sedval _ = testdot ``(\x. x + y) 5`` ``5 + y``;
127193323Sedval _ = testdot ``(\x. x + x + 1) ((\y. y + 10) 4)`` ``29``;
128193323Sed