-
Notifications
You must be signed in to change notification settings - Fork 0
/
prufrepl.sml
223 lines (186 loc) · 4.46 KB
/
prufrepl.sml
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
(* I have measured out my life with coffee spoons... *)
datatype REPLCmd
= RComment
| RCheck of Term
| RShow of Name
| RAssert of Name * Term
| RDefine of Name * Term
| RDump
| RRand
| RConflict
local
open StringParser
in
val p_namechar = choose
(map literally
(String.explode "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_"))
val p_name = many1 p_namechar @@ String.implode
fun p_term str =
( p_prop
|| p_type
|| p_pi
|| p_lambda
|| p_app
|| p_var
|| p_parens
) str
and p_prop str = ($"Prop" @@ (fn _ => TProp)) str
and p_type str = ($"Type" ~ ws1 ~ num_int
@@ (fn (_, (_, n)) => TType n)) str
and p_var str = ($"$" ~ p_name @@ (fn (_, n) => TVar n)) str
and p_pi str = (
( $"pi"
~ ws1
~ p_name
~ ws
~ $":"
~ ws
~ p_term
~ ws
~ $"=>"
~ ws
~ p_term
)
@@ (fn (_,(_,(n,(_,(_,(_,(t,(_,(_,(_,v)))))))))) => TPi (n, t, v))
) str
and p_lambda str = (
( $"lambda"
~ ws1
~ p_name
~ ws
~ $":"
~ ws
~ p_term
~ ws
~ $"=>"
~ ws
~ p_term
)
@@ (fn (_,(_,(n,(_,(_,(_,(t,(_,(_,(_,v)))))))))) => TLambda (n, t, v))
) str
and p_app str = (
( $"<"
~ ws
~ p_term
~ ws1
~ p_term
~ ws
~ $">"
)
@@ (fn (_,(_,(f,(_,(a,(_,_)))))) => TApp (f, a))
) str
and p_parens str = (
( $"("
~ ws
~ p_term
~ ws
~ $")"
)
@@ (fn (_,(_,(t,(_,_)))) => t)
) str
val p_cmt = $"#" ~ (many any) @@ (fn _ => ())
val p_command =
( ws ~ $"Check" ~ ws1 ~ p_term ~ ws ~ (maybe p_cmt)
@@ (fn (_,(_,(_,(t,_)))) => RCheck t)
|| ws ~ $"Show" ~ ws1 ~ p_name ~ ws ~ p_cmt
@@ (fn (_,(_,(_,(n,_)))) => RShow n)
|| ws ~ $"Assert" ~ ws1 ~ p_name ~ ws1 ~ p_term ~ ws ~ (maybe p_cmt)
@@ (fn (_,(_,(_,(n,(_,(t,_)))))) => RAssert (n, t))
|| ws ~ $"Define" ~ ws1 ~ p_name ~ ws1 ~ p_term ~ ws ~ (maybe p_cmt)
@@ (fn (_,(_,(_,(n,(_,(t,_)))))) => RDefine (n, t))
|| ws ~ $"Dump" ~ ws ~ (maybe p_cmt)
@@ (fn _ => RDump)
|| ws ~ $"Rand" ~ ws ~ (maybe p_cmt)
@@ (fn _ => RRand)
|| ws ~ $"Conflict" ~ ws ~ (maybe p_cmt)
@@ (fn _ => RConflict)
|| ws ~ (maybe p_cmt)
@@ (fn _ => RComment)
)
end;
val urandom = BinIO.openIn("/dev/urandom");
fun do_rand e =
let
fun rand_byte () : int =
Word8.toInt (valOf (BinIO.input1 urandom))
;
fun rand_int () : int =
(rand_byte ()) +
(rand_byte () * 0x100) +
(rand_byte () * 0x10000)
fun rand_key e =
let val e' = env.to_raw e in
#1 (List.nth(e', rand_int () mod length e'))
end
;
fun try_rand_pair e =
let val f = rand_key e;
val a = rand_key e;
val t = TApp (TVar f, TVar a)
in
(SOME (f, a, typecheck t e))
handle TypeError s => NONE
end
;
fun get_rand_pair e =
case try_rand_pair e of
SOME x => x
| NONE => get_rand_pair e
in
let val (f, a, t) = get_rand_pair e;
val n = (f^" ["^a^"]") in
env.insert e n t
end
end
fun do_conflict (e : Env) =
if List.exists (fn x => x = TVar "conflict")
(map #2 (env.to_raw e))
then e else do_conflict (do_rand e)
fun apply_cmd (cmd : REPLCmd) (e : Env) =
case cmd of
RComment => e
| RCheck t => (
(print ((string_of_term (typecheck t e))^"\n") ; e)
handle TypeError s => (print ("Type error: "^s^"\n") ; e)
)
| RShow n => (
case env.lookup e n of
NONE =>
(print "Name not found.\n" ; e)
| SOME t =>
(print ((string_of_term t)^"\n") ; e)
)
| RAssert (n, t) =>
env.insert e n t
| RDefine (n, t) => (
env.insert e n (typecheck t e)
handle TypeError s => (print ("Type error: "^s^"\n") ; e)
)
| RDump => (
print (string_of_env e) ;
print "\n" ;
e
)
| RRand => do_rand e
| RConflict => do_conflict e
fun repl e = (
(if Posix.ProcEnv.isatty Posix.FileSys.stdin then print ">> " else ()) ;
let val inp = TextIO.inputLine TextIO.stdIn in
case inp of
NONE => ()
| SOME line =>
case p_command (String.explode line) of
NONE => (
print "Syntax error.\n" ;
repl e
)
| SOME (cmd, []) => (
repl (apply_cmd cmd e)
)
| SOME (_, _) => (
print "Syntax error.\n" ;
repl e
)
end
);
repl env.empty;