-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.lean
154 lines (135 loc) · 3.46 KB
/
Main.lean
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import «Pfmt»
open Pfmt
def assertEq (lhs rhs : String) : IO Bool := do
if lhs == rhs then
return true
else
IO.println s!"Strings are not equal:\nLeft:\n{lhs}\nRight:\n{rhs}"
return false
def printDocChoice (w : Nat) : String :=
let exit_d := .text "exit();"
let d :=
.text "while (true) {" ++
.nest 4
(.nl ++ .text "f();" ++ .nl ++ .text "if (done())" ++
(
(.space ++ exit_d) <|||>
(.nest 4
(.nl ++ exit_d))
)
) ++
.nl ++ .text "}"
Doc.prettyPrint (χ := DefaultCost) d 0 w
def printDocGroup (w : Nat) : String :=
let d :=
.text "while (true) {" ++
.nest 4
(.nl ++ .text "f();" ++ .nl ++ .text "if (done())" ++
.group
(.nest 4
(.nl ++ .text "exit();")
)
) ++ .nl ++ .text "}"
Doc.prettyPrint (χ := DefaultCost) d 0 w
def test_choice_doc_80 : IO Bool :=
assertEq
(String.intercalate "\n"
[ "while (true) {"
, " f();"
, " if (done()) exit();"
, "}"
])
(printDocChoice 80)
def test_choice_doc_20 : IO Bool :=
assertEq
(String.intercalate "\n"
[ "while (true) {"
, " f();"
, " if (done())"
, " exit();"
, "}"
])
(printDocChoice 20)
def test_group_doc_80 : IO Bool :=
assertEq
(String.intercalate "\n"
[ "while (true) {"
, " f();"
, " if (done()) exit();"
, "}"
])
(printDocGroup 80)
def test_group_doc_20 : IO Bool :=
assertEq
(String.intercalate "\n"
[ "while (true) {"
, " f();"
, " if (done())"
, " exit();"
, "}"
])
(printDocGroup 20)
inductive Sexp where
| atom (s : String)
| list (ss : List Sexp)
partial def Sexp.toDoc (s : Sexp) : Doc :=
let acat := Doc.fold (fun x y => x <+> .space <+> y)
match s with
| .atom s => .text s
| .list [] => .lparen <+> .rparen
| .list [x] => .lparen <+> x.toDoc <+> .rparen
| .list (x :: xs) =>
let xDoc := x.toDoc
let xsDoc := xs.map Sexp.toDoc
.lparen <+>
(acat (xDoc :: xsDoc) <|||> -- the horizontal style
.lines (xDoc :: xsDoc) <|||> -- the vertical style
(xDoc <+> .space <+> .lines xsDoc)) <+> -- the argument list style
.rparen
def Sexp.prettyPrint (s : Sexp) (w : Nat) : String := s.toDoc.prettyPrint DefaultCost 0 w
def Sexp.example : Sexp := list [atom "a", atom "b", atom "c", atom "d"]
def test_sexp_4 : IO Bool :=
assertEq
(String.intercalate "\n"
[ "(a"
, " b"
, " c"
, " d)"
])
(Sexp.example.prettyPrint 4)
def test_sexp_6 : IO Bool :=
assertEq
(String.intercalate "\n"
[ "(a b"
, " c"
, " d)"
])
(Sexp.example.prettyPrint 6)
def test_sexp_10 : IO Bool :=
assertEq
(String.intercalate "\n"
[ "(a b c d)" ])
(Sexp.example.prettyPrint 10)
def runTests (tests : List (String × IO Bool)) : IO Bool := do
for (name, test) in tests do
if ← test then
IO.println s!"{name}: SUCCESS"
else
IO.println s!"{name}: FAILURE"
return false
return true
def main : IO UInt32 := do
let ret ← runTests [
("choice 80", test_choice_doc_80),
("choice 20", test_choice_doc_20),
("group 80", test_group_doc_80),
("group 20", test_group_doc_20),
("sexp 4", test_sexp_4),
("sexp 6", test_sexp_6),
("sexp 10", test_sexp_4),
]
if ret then
return 0
else
return 1
#check Nat.rec