aboutsummaryrefslogtreecommitdiff
path: root/Smurf.icl
blob: f2ab10c96f39438876270570695659bf025b099b (plain) (blame)
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
implementation module Smurf

from StdFunc import o, flip
import StdArray
import StdDebug
import StdList
import StdMisc
import StdString
import StdTuple
import StdFile

from Data.Func import $
import Control.Applicative
import Control.Monad
import Data.Maybe
import Data.List
from Text import class Text(concat), instance Text String

import GenEq
import GenPrint

import SmurfParse
import LaTeX

derive gEq Stm, Expr, State
derive gPrint (,), Expr, VarChar

instance == Stm where == a b = a === b
instance == Expr where == a b = a === b
instance == State where == a b = a === b

instance zero [a] where zero = []

/// Utilities ...

(<+) infixr 5 :: a b -> String | toString a & toString b
(<+) a b = toString a +++ toString b

instance toString Expr
where
	toString (Lit s)      = s
	toString (Var v)      = "$" <+ v
	toString (ECat a b)   = a <+ b
	toString (EHead a)    = "hd(" <+ a <+ ")"
	toString (ETail a)    = "tl(" <+ a <+ ")"
	toString (EQuotify a) = "q(" <+ a <+ ")"

instance toString Stm
where
    toString (Push s) = toString $ quotify s
    toString Input = "i"
    toString Output = "o"
    toString Cat = "+"
    toString Head = "h"
    toString Tail = "t"
    toString Quotify = "q"
    toString Put = "p"
    toString Get = "g"
    toString Exec = "x"

instance toChar Stm where toChar stm = (toString stm).[0]

instance fromChar Stm
where
	fromChar '"' = Push $ Lit ""
	fromChar 'i' = Input
	fromChar 'o' = Output
	fromChar '+' = Cat
	fromChar 'h' = Head
	fromChar 't' = Tail
	fromChar 'q' = Quotify
	fromChar 'p' = Put
	fromChar 'g' = Get
	fromChar 'x' = Exec

printList :: [a] -> String | toString a
printList xs = "[" <+ concat (intersperse ":" (map (\s->'"' <+ s <+ '"') xs)) <+ "]"

instance zero State where zero = { stack = zero, store = zero }

instance zero ListIO where zero = {input=[], output=[]}

instance toString State
where
	toString {stack,store} = "(" <+ printToString stack <+ "," <+ printToString store <+ ")"

instance toString Transition
where
	toString ((p1, ip1, st1) --> (ip2, op, st2))
		= "<" <+ simple 2 p1 <+ "," <+ st1 <+ "," <+ printList ip1 <+ "> -> ("
			<+ printList ip2 <+ "," <+ printList op <+ "," <+ st2 <+ ")"
	where
		simple :: !Int !Program -> String
		simple _ [] = "λ"
		simple i pgm
			| i >= length pgm = concat $ intersperse ":" $ map toString pgm
			= simple i (take i pgm) +++ ":..."

instance toString DerivationTree
where toString ts = concat $ intersperse "\n" $ map toString $ reverse ts

instance toLaTeX Stm
where
	toLaTeX (Push s) = List [Command "StmPush" [], Raw "~", eToLaTeX s]
	toLaTeX Input    = Command "StmInput" []
	toLaTeX Output   = Command "StmOutput" []
	toLaTeX Cat      = Command "StmCat" []
	toLaTeX Head     = Command "StmHead" []
	toLaTeX Tail     = Command "StmTail" []
	toLaTeX Quotify  = Command "StmQuotify" []
	toLaTeX Put      = Command "StmPut" []
	toLaTeX Get      = Command "StmGet" []
	toLaTeX Exec     = Command "StmExec" []

instance toLaTeX Program
where
	toLaTeX []  = Command "lambda" []
	toLaTeX pgm = List $ intersperse (Text ":") (map toLaTeX pgm)

instance toLaTeX String where toLaTeX s = Text s

instance toLaTeX Store
where
	toLaTeX [] = Command "emptyset" []
	toLaTeX ass
		= List
			([Command "{" []] ++
			intersperse (Raw ",") (map assToLaTeX ass) ++
			[Command "}" []])
	where
		assToLaTeX :: (Expr,Expr) -> LaTeX
		assToLaTeX (var,val)
			= List
				[ eToLaTeX var
				, Command "mapsto" []
				, eToLaTeX val
				]

instance toLaTeX State
where
	toLaTeX {stack,store}
		= List
			[ Command "left" []
			, Raw "("
			, stackToLaTeX stack
			, Raw ","
			, toLaTeX store
			, Command "right" []
			, Raw ")"
			]

instance toLaTeX Transition
where
	toLaTeX ((p1,ip1,st1) --> (ip2,op,st2))
		= Command "trans"
			[ toLaTeX p1
			, stackToLaTeX ip1
			, toLaTeX st1
			, stackToLaTeX ip2
			, stackToLaTeX op
			, toLaTeX st2
			]

stackToLaTeX :: Stack -> LaTeX
stackToLaTeX es
	= List $ intersperse (Text ":") (map eToLaTeX es ++ [Command "Nil" []])

eToLaTeX :: Expr -> LaTeX
eToLaTeX e = Command "texttt" [List [Raw "\"", etl e, Raw "\""]]
where
	etl :: Expr -> LaTeX
	etl (Lit s)      = Text s
	etl (Var v)      = Math False [Raw v]
	etl (ECat a b)   = List [etl a, etl b]
	//etl (ECat a b)   = List [etl a, Raw "+(", etl b, Raw ")"]
	etl (EHead e)    = List [Command "textit" [Text "hd"], Raw "(", etl e, Raw ")"]
	etl (ETail e)    = List [Command "textit" [Text "tl"], Raw "(", etl e, Raw ")"]
	etl (EQuotify e) = List [Command "textit" [Text "q"], Raw "(", etl e, Raw ")"]

instance toLaTeX DerivationTree
where
	toLaTeX ts = toL ts
	where
		toL :: DerivationTree -> LaTeX
		toL []  = List []
		toL [t]
			= List
				[ Command "axjustifies" []
				, toLaTeX t
				, Command "using" [rule]
				]
		where
			rule = case t of
				(([], _, _) --> (_, _, _)) = Command "rlambdans" []
				_ = Command "rule" [Text "assumption", Text ""]
		toL [t=:((p1,_,_)-->_):ts]
			= List
				[ Command "[" []
				, toL ts
				, Command "]" []
				, Command "justifies" []
				, toLaTeX t
				, Command "using" [Command rule []]
				]
		where
			rule = case p1 of
				[Push _:_]  = "rpushns"
				[Input:_]   = "rinputns"
				[Output:_]  = "routputns"
				[Cat:_]     = "rcatns"
				[Head:_]    = "rheadns"
				[Tail:_]    = "rtailns"
				[Quotify:_] = "rquotifyns"
				[Put:_]     = "rputns"
				[Get:_]     = "rgetns"
				[Exec:_]    = "rexecns"

/// End utilities

instance +++ Expr
where
	(+++) (Lit s) (Lit t) = Lit $ s +++ t
	(+++) a b             = ECat a b

run :: !Program State io (IO io) -> (Maybe State, io)
run prog st io iofuncs
    # (mbProgSt, io) = step prog st io iofuncs
    | isNothing mbProgSt = (Nothing, io)
    # (prog, st) = fromJust mbProgSt
    = if (isEmpty prog) (Just st, io) (run prog st io iofuncs)

step :: !Program State u:io u:(IO u:io) -> u:(Maybe (!Program, State), u:io)
step [] st io _
    = (pure ([], st), io)
step [Push s:p] st io _
    = (pure (p, { st & stack = push s st.stack }), io)
step [Input:p] st io (IO input _)
	# (ip, io) = input io
    = (pure (p, { st & stack = push ip st.stack }), io)
step [Output:p] st io (IO _ output)
    # mbSStk = pop st.stack
    | isNothing mbSStk = (empty, io)
    # (s, stk) = fromJust mbSStk
    = (pure (p, { st & stack = stk }), output s io)
step [Cat:p] st io _
    = (pop st.stack >>= \(x,stk) -> pop stk >>= \(y,stk`) ->
        pure (p, { st & stack = push (y +++ x) stk` }), io)
step [Head:p] st io _
    = (pop st.stack >>= \(x,stk) -> head x >>= \x` ->
        pure (p, { st & stack = push x` stk }), io)
step [Tail:p] st io _
    = (pop st.stack >>= \(x,stk) -> tail x >>= \x` ->
        pure (p, { st & stack = push x` stk }), io)
step [Quotify:p] st io _
    = (pop st.stack >>= \(x,stk) ->
        pure (p, { st & stack = push (quotify x) stk }), io)
step [Put:p] st io _
    = (pop st.stack >>= \(var,stk) -> pop stk >>= \(val,stk`) ->
        pure (p, { st & stack = stk`, store = put var val st.store }), io)
step [Get:p] st io _
    = (pop st.stack >>= \(var,stk) ->
        pure (p, { st & stack = push (get var st.store) stk }), io)
step [Exec:p] st io _
    = (pop st.stack >>= \(pgm,_) -> pure (let vcs = eToVarChars (trace pgm pgm) in fromJust $ parsev $ trace (printToString vcs +++ "\n") vcs, zero), io)

push :: Expr Stack -> Stack
push s st = [s:st]

pop :: Stack -> Maybe (Expr, Stack)
pop [] = empty
pop [s:ss] = pure (s, ss)

head :: Expr -> Maybe Expr
head (Lit "") = empty
head (Lit s) = pure $ Lit $ s % (0,0)
head (ECat a b) = head a <|> head b
head e = pure $ EHead e

tail :: Expr -> Maybe Expr
tail (Lit "") = empty
tail (Lit s) = pure $ Lit $ s % (1, size s - 1)
tail e = pure $ ETail e

put :: Expr Expr Store -> Store
put var val store = [(var,val) : filter ((<>)var o fst) store]

get :: Expr Store -> Expr
get var store = case filter ((==)var o fst) store of
	[]          = Lit ""
	[(_,val):_] = val

quotify :: Expr -> Expr
quotify e = ECat (Lit "\"") (ECat (quotify` e) (Lit "\""))
where
	quotify` (Lit s) = Lit $ toString $ quot $ fromString s
	where
	    quot :: [Char] -> [Char]
	    quot []        = []
	    quot ['\\':cs] = ['\\':'\\':quot cs]
	    quot ['\n':cs] = ['\\':'n':quot cs]
	    quot ['\r':cs] = ['\\':'r':quot cs]
	    quot ['\t':cs] = ['\\':'t':quot cs]
	    quot ['"':cs]  = ['\\':'"':quot cs]
	    quot [c:cs]    = [c:quot cs]
	quotify` (ECat a b) = ECat (quotify` a) (quotify` b)
	quotify` (EHead a)  = EHead (quotify` a)
	quotify` (ETail a)  = ETail (quotify` a)
	quotify` (Var v)    = EQuotify (Var v)

listIO :: IO ListIO
listIO = IO read write
where
	read :: ListIO -> (Expr, ListIO)
	read io=:{input} = (hd input, { io & input=tl input })

	write :: Expr ListIO -> ListIO
	write s io=:{output} = { io & output=output ++ [s] }

prover :: ((Program, Stack, State) -> Maybe (Stack, Stack, State))
	Program State ListIO (IO ListIO) -> Maybe DerivationTree
prover assumptions pgm st io iof
# init              = (pgm, io.input, st)
# mbAssumption      = assumptions init
| isJust mbAssumption
	= Just [init --> fromJust mbAssumption]
# (mbPgmSt, io)     = step pgm st io iof
| isNothing mbPgmSt = empty
# (pgm, st)         = fromJust mbPgmSt
| isEmpty pgm       = pure [ init --> (io.input, io.output, st)
                           , ([],io.input,st) --> (io.input,[],st)
                           ]
# mbTree            = prover assumptions pgm st io iof
| isNothing mbTree  = empty
# [child=:(_ --> final):children] = fromJust mbTree
= pure [init --> final:child:children]