-
Notifications
You must be signed in to change notification settings - Fork 0
/
cbpv.lhs
797 lines (630 loc) · 22.2 KB
/
cbpv.lhs
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
> {-# LANGUAGE GADTs #-}
> {-# LANGUAGE TypeOperators #-}
> {-# LANGUAGE FlexibleInstances #-}
> {-# LANGUAGE FlexibleContexts #-}
> {-# LANGUAGE DeriveFunctor #-}
> {-# LANGUAGE MultiParamTypeClasses #-}
> import CompClasses3
-- syntax
> type Name = String
> type EMsg = String
-- toy CBPV language with exceptions
> data Expr
> -- value types
> = Val Int -- integer constant
> | Vars Name -- variable
> | Thunk Expr -- thunk of a computation
> | ThrowE EMsg -- exception throwing
> -- computation types
> | Add Expr Expr -- sum of two ints
> | Force Expr -- forcing a thunk to run
> | Return Expr -- value returning computation
> | Do Name Expr Expr -- sequencing |do n <- e1 in e2|
> | Let Name Expr Expr -- let-binding |let n = e1 in e2|
> | Fun Name Expr -- function |\ x -> e|
> | Apply Expr Expr -- application |f v|
> | CatchE Expr Expr -- exception catching
> deriving Show
-- semantic domain of CBPV with exceptions
> data ExprValue
> = VNum Int -- Integer literal
> | VThunk Env Expr -- thunk value
> | VThunk' Env Code -- thunk value with compiled computation
> | VReturn ExprValue -- return value
> | VFun Env Name Expr -- function closure returned
> | VFun' Env Name Code -- abstraction with compiled body + closure
> deriving Show
-- aux types and terms
> type Env = [(Name, ExprValue)]
> emptyEnv = []
> type Stack = [ExprValue]
> emptyStack = []
> type Conf = (Stack, Env)
> emptyConf = (emptyStack,emptyEnv)
-- concrete semantics of cbpv with exceptions
> eval0 :: Expr -> Env -> Either EMsg ExprValue
> -- value types
> eval0 (Val n) env = Right (VNum n)
> eval0 (Vars n) env = case lookup n env of
> Nothing -> Left ("Unknown Variable" ++ show n)
> Just v -> Right v
> eval0 (Thunk e1) env = Right (VThunk env e1)
> eval0 (ThrowE msg) env = Left msg
> -- computation types
> eval0 (Add e1 e2) env = case eval0 e1 env of
> Right (VNum m) -> case eval0 e2 env of
> Right (VNum n) -> Right (VNum (m+n))
> Left e -> Left ("Integers Expected in Addition " ++ e)
> Left e -> Left ("Integers Expected in Addition " ++ e)
> eval0 (Force e1) env = case eval0 e1 env of
> Right (VThunk env' e2) -> eval0 e2 env'
> Left e -> Left ("Thunk Expected in Force " ++ e)
> eval0 (Return e1) env = let Right v1 = eval0 e1 env in
> Right (VReturn v1)
> eval0 (Do n e1 e2) env = case eval0 e1 env of
> Right (VReturn v) -> eval0 e2 ((n,v):env)
> Left e -> Left ("return expected " ++ e)
> eval0 (Let n e1 e2) env = let Right v = eval0 e1 env in
> eval0 e2 ((n, v):env)
> eval0 (Fun n e1) env = Right (VFun env n e1)
> eval0 (Apply e1 e2) env = case eval0 e1 env of
> Right (VFun env' n e3) -> let Right v = eval0 e2 env in
> eval0 e3 ((n, v):env')
> Left e -> Left ("function expected" ++ e)
> eval0 (CatchE e1 e2) env = case eval0 e1 env of
> Left e -> case eval0 e2 env of
> Left e' -> Left ("catch and exception failed" ++ e' ++ e)
> r -> r
> r -> r
> test1 = (Fun "n" (Return (Add (Vars "n") (Val 3))))
> test2 = (Return (Add (Val 1) (Val 2)))
> test3 = (Force (Thunk (Return (Val 1))))
> test4 = (Let "x" (Add (Val 3) (Val 5)) (Return (Add (Vars "x") (Vars "x"))))
> test5 = (Do "x" (Return (Add (Val 3) (Val 5))) (Return (Add (Vars "x") (Vars "x"))))
> test6 = (Add (Val 2) (ThrowE "we have an error"))
> test7 = CatchE (Add (Val 2) (ThrowE "we have an error")) (Val 3)
{-
Main> eval0 test1 [("v", (VNum 3))]
Right (VFun [("v",VNum 3)] "n" (Return (Add (Var "n") (Val 3))))
Main> eval0 test2 emptyEnv
Right (VReturn (VNum 3))
Main> eval0 test3 emptyEnv
Right (VReturn (VNum 1))
Main> eval0 test4 emptyEnv
Right (VReturn (VNum 16))
Main> eval0 test5 emptyEnv
Right (VReturn (VNum 16))
Main> eval0 test6 emptyEnv
Left "Integers Expected in Addition we have an error"
Main> eval0 test7 emptyEnv
Right (VNum 3)
-}
{-massage into Expr -> Conf -> Conf form-}
> eval1 :: Expr -> Conf -> Either EMsg Conf
> -- value types
> eval1 (Val n) (s,env) = Right (VNum n:s, env)
> eval1 (Vars n) (s,env) = case lookup n env of
> Nothing -> Left ("Unknown Variable" ++ show n)
> Just v -> Right (v:s, env)
> eval1 (Thunk e1) (s,env) = Right ((VThunk env e1):s, env)
> eval1 (ThrowE msg) c = Left msg
> -- computation types
> eval1 (Add e1 e2) c = case eval1 e1 c of
> Right c' -> case eval1 e2 c' of
> Right ((VNum m):(VNum n):s, env) -> Right ((VNum (m+n)):s, env)
> Left e -> Left ("Integers Expected in Addition " ++ e)
> Left e -> Left ("Integers Expected in Addition " ++ e)
> eval1 (Force e1) c = case eval1 e1 c of
> Right ((VThunk env'' e2):s', env') -> eval1 e2 (s', env'')
> Left e -> Left ("Thunk Expected in Force " ++ e)
> eval1 (Return e1) c@(s,env) = case eval1 e1 c of
> Right (v:s, env) -> Right ((VReturn v):s, env)
> l -> l
> eval1 (Do n e1 e2) c@(s,env) = case eval1 e1 c of
> Right ((VReturn v):s, env) -> eval1 e2 (s, (n,v):env)
> Left e -> Left ("return expected " ++ e)
> eval1 (Let n e1 e2) c@(s,env)= let Right ((v:vs), env') = eval1 e1 c in
> eval1 e2 (s,(n, v):env)
> eval1 (Fun n e1) c@(s,env) = Right ((VFun env n e1):s, env)
> eval1 (Apply e1 e2) c = case eval1 e1 c of
> Right ((VFun env' n e3):s, env) -> let Right (v:vs, env'') = eval1 e2 c in
> eval1 e3 (s, (n, v):env')
> Left e -> Left ("function expected" ++ e)
> eval1 (CatchE e1 e2) c = case eval1 e1 c of
> Left e -> case eval1 e2 c of
> Left e' -> Left ("Catch and Exception Failed" ++ e' ++ e)
> r -> r
> r -> r
{-
Main> eval1 test1 (emptyStack, [("v", (VNum 3))])
Right ([VFun [("v",VNum 3)] "n" (Return (Add (Vars "n") (Val 3)))],[("v",VNum 3)])
Main> eval1 test2 emptyConf
Right ([VReturn (VNum 3)],[])
Main> eval1 test3 emptyConf
Right ([VReturn (VNum 1)],[])
Main> eval1 test4 emptyConf
Right ([VReturn (VNum 16)],[("x",VNum 8)])
Main> eval1 test5 emptyConf
Right ([VReturn (VNum 16)],[("x",VNum 8)])
Main> eval1 test6 emptyConf
Left "Integers Expected in Addition we have an error"
Main> eval1 test7 emptyConf
Right ([VNum 3],[])
-}
{-define algebraic handlers for CBPV with exceptions-}
{-stack functor to store resultant stack values-}
{-same as stack functor from Exc example-}
> data StackFunctor s a
> = PopS (s -> a)
> | PushS s a
> deriving Functor
> type HStack s = Lift (StackFunctor s)
-- popS = pop Stack, pushS = push Stack
> popS :: (HStack ExprValue :<: g)=>
> Free g ExprValue
> popS = inject (Lift (PopS Var))
> pushS :: (HStack ExprValue :<: g)=>
> ExprValue -> Free g ()
> pushS v = inject (Lift (PushS v (Var ())))
-- open handler for stack functor
> handleStackOpen :: Syntax g =>
> [v] ->
> Free (HStack v :+: g) a ->
> Free g ([v], a)
> handleStackOpen s (Var a) = return (s,a)
> handleStackOpen [] (Cons (Inl (Lift (PopS k)))) = error "Cannot Pop Empty Stack"
> handleStackOpen (x:xs) (Cons (Inl (Lift (PopS k)))) = handleStackOpen xs (k x)
> handleStackOpen xs (Cons (Inl (Lift (PushS x k)))) = handleStackOpen (x:xs) k
> handleStackOpen s (Other op) =
> Cons (weave (s,()) (uncurry handleStackOpen) op)
{-Exception functor to store resultant stack values-}
{-upgraded so that throw now supports error logging-}
{-CBPV is really good for error tracing-}
> data ExcFunctor e m a where
> Throw :: e -> ExcFunctor e m a
> Catch :: (m x) -> (e -> m x) -> (x -> m a) -> ExcFunctor e m a
> instance Functor m => Functor (ExcFunctor e m) where
> fmap f (Throw e) = (Throw e)
> fmap f (Catch p h k) = Catch p h (fmap f . k)
> type HExc = ExcFunctor
-- standard tricks to capture tree notation
> throw ::(HExc EMsg :<: g) =>
> EMsg -> Free g a
> throw e = inject (Throw e)
> catch ::(HExc EMsg :<: g) =>
> Free g a ->
> (EMsg -> Free g a) ->
> Free g a
> catch p h = inject (Catch p h return)
> instance HFunctor (HExc e) where
> hmap t (Throw e) = Throw e
> hmap t (Catch p h k) = Catch (t p) (t . h) (t . k)
> instance Syntax (HExc e) where
> emap f (Throw e) = Throw e
> emap f (Catch p h k) = Catch p h (f . k)
> weave f hdl (Throw e) = Throw e
> weave f hdl (Catch p h k) =
> Catch (hdl (fmap (const p) f))
> (\e -> hdl (fmap (const (h e)) f))
> (hdl . fmap k)
--Higher order open handler for exceptions
-- now uses EITHER type, reflective of concrete semantics
-- and Exc e m a type
> handleExc :: (Syntax g, Monoid e) =>
> Free (HExc e :+: g) a ->
> Free g (Either e a)
> handleExc (Var x) = return (Right x)
> handleExc (Cons (Inl (Throw e))) = return (Left e)
> handleExc (Cons (Inl (Catch p h k))) = do
> r <- handleExc p
> case r of
> (Right x) -> handleExc (k x)
> (Left e) -> do
> r <- handleExc (h e)
> case r of
> (Right x) -> handleExc (k x)
> (Left e') -> return (Left (mappend e' e))
> handleExc (Other op) = Cons (weave (Right ()) hdl op) where
> hdl = either (return . Left) handleExc
-- HFunctor for pure computations
> data Void k deriving Functor
> type HVoid = Lift Void
> handleVoid :: Free HVoid a -> a
> handleVoid (Var a) = a
-- State Functor for Environment
-- forms the state computational effect with GET and PUT
> data StateFunctor s a
> = Get (s -> a)
> | Put s a
> deriving Functor
> type HState s = Lift (StateFunctor s)
-- standard trick to capture tree notation of state functor
> get :: (HState e :<: g)=>
> Free g e
> get = inject (Lift (Get Var))
> put :: (HState e :<: g)=>
> e -> Free g ()
> put e = inject (Lift (Put e (Var ())))
-- additional abuse of notation to simulate a stack using state
> pushE :: (HState [v] :<: g)=>
> v -> Free g ()
> pushE x = do {env<-get; put (x:env)}
-- handle with catch if used
-- open handler for state effect
> handleStateOpen :: Syntax g =>
> e ->
> Free (HState e :+: g) a ->
> Free g (e, a)
> handleStateOpen env (Var a) = return (env,a)
> handleStateOpen env (Cons (Inl (Lift (Get k)))) = handleStateOpen env (k env)
> handleStateOpen env (Cons (Inl (Lift (Put env' k)))) = handleStateOpen env' k
> handleStateOpen env (Other op) =
> Cons (weave (env,()) (uncurry handleStateOpen) op)
-- abstract evaluation semantics for CBPV with Exceptions, variables,
-- and recursive functions
-- (could add bools, sum, products but it wouldn't add to the proof really)
> eval' :: (HStack ExprValue :<: g, HExc EMsg :<: g, HState Env :<: g) =>
> Expr ->
> Free g () ->
> Free g ()
> -- value types
> eval' (Val n) c = do
> c
> pushS (VNum n)
> eval' (Vars n) c = do
> c
> env <- get
> case lookup n env of
> Nothing -> throw ("Unknown Variable " ++ n)
> Just v -> pushS v
> eval' (Thunk e1) c = do
> c
> env <- get
> pushS (VThunk env e1)
> eval' (ThrowE e) c = throw e
> -- computation types
> eval' (Add e1 e2) c = do
> eval' e2 (eval' e1 c)
> v1 <- popS
> v2 <- popS
> case (v1, v2) of
> ((VNum n), (VNum m)) -> pushS (VNum (m+n))
> _ -> throw ("VNums Expected in Addition " ++ show v1 ++ " " ++ show v2 ++ " received")
> eval' (Force e1) c = do
> eval' e1 c
> v <- popS
> case v of
> (VThunk env e) -> do
> let c' = put env
> eval' e c'
> _ -> throw ("VThunk expected in Force, " ++ show v ++ " received")
> eval' (Return e1) c = do
> eval' e1 c
> v <- popS
> pushS (VReturn v)
> eval' (Do n e1 e2) c = do
> eval' e1 c
> v1 <- popS
> case v1 of
> (VReturn v2) -> do
> let c' = pushE (n,v2)
> eval' e2 c'
> _ -> throw ("VReturn expected in Do, " ++ show v1 ++ " received")
> eval' (Let n e1 e2) c = do
> eval' e1 c
> v <- popS
> let c' = pushE (n,v)
> eval' e2 c'
> eval' (Fun n e1) c = do
> c
> env <- get
> pushS (VFun env n e1)
> eval' (Apply e1 e2) c = do
> eval' e2 (eval' e1 c)
> v <- popS
> f <- popS
> case f of
> (VFun env' n f) -> do
> let c'' = put ((n,v):env')
> eval' f c''
> _ -> throw ("VFun function expected " ++ show f ++ " received")
> eval' (CatchE e1 e2) c = catch (eval' e1 c) (\e -> catch (eval' e2 c)
> (\e' -> throw ("Catch and Exception Failed " ++ e' ++ e)))
> handler stack env = (handleVoid . handleStackOpen stack . handleStateOpen env . handleExc)
> defaultHandler = handler (emptyStack::Stack) (emptyEnv::Env)
> initConf :: Syntax g => Free g ()
> initConf = return ()
{-
Main> defaultHandler (eval' test1 initConf)
([VFun [] "n" (Return (Add (Vars "n") (Val 3)))],([],Right ()))
Main> defaultHandler (eval' test2 initConf)
([VReturn (VNum 3)],([],Right ()))
Main> defaultHandler (eval' test3 initConf)
([VReturn (VNum 1)],([],Right ()))
Main> defaultHandler (eval' test4 initConf)
([VReturn (VNum 16)],([("x",VNum 8)],Right ()))
Main> defaultHandler (eval' test5 initConf)
([VReturn (VNum 16)],([("x",VNum 8)],Right ()))
-}
-- conversion function
> conv :: ExprValue -> ExprValue
> conv (VThunk env e1) = VThunk' (conve env) (comp1 e1)
> conv (VReturn v) = VReturn (conv v)
> conv (VFun env n e1) = VFun' (conve env) n (comp1 e1)
> conv x = x
> conve :: Env -> Env
> conve = fmap (\(n,v) -> (n, conv v))
--
correctness specification is now
exec (comp s t) c = exec t (eval s c[conv v/v]), if s evals to v in config c
{-target machine code, derived-}
> data Code where
> HALT :: Code
> PUSH :: ExprValue -> Code -> Code
> LOOKUP :: Name -> Code -> Code
> FAIL :: EMsg -> Code
> THK :: Code -> Code -> Code
> ADD :: Code -> Code
> FRC :: Code -> Code
> RET :: Code -> Code
> APP :: Code -> Code
> FUN :: Name -> Code -> Code -> Code
> DO :: Name -> Code -> Code -> Code
> LET :: Name -> Code -> Code -> Code
> deriving Show
{-virtual machine code, derived-}
> exec' :: (HStack ExprValue :<: g, HExc EMsg :<: g, HState Env :<: g) =>
> Code ->
> Free g () ->
> Free g ()
> exec' HALT c = c
> exec' (PUSH v t) c = exec' t (do {c; pushS v})
> exec' (LOOKUP n t) c = exec' t (do
> c
> env <- get
> case lookup n env of
> Nothing -> throw ("Unknown Variable " ++ n)
> Just v -> pushS v)
> exec' (FAIL e) c = throw e
> exec' (ADD t) c = exec' t (do
> c
> v1 <- popS
> v2 <- popS
> case (v1, v2) of
> ((VNum n), (VNum m)) -> pushS (VNum (m+n))
> _ -> throw ("VNums Expected in Addition " ++ show v1 ++ " " ++ show v2 ++ " received"))
> exec' (FRC t) c = exec' t (do
> c
> v <- popS
> case v of
> (VThunk' env t1) -> do
> let c' = put env
> exec' t1 c'
> _ -> throw ("VThunk expected in Force, " ++ show v ++ " received"))
> exec' (RET t) c = exec' t (do
> c
> v <- popS
> pushS (VReturn (conv v)))
> exec' (APP t) c = exec' t (do -- ! JEREMY WHAT DO YOU THINK
> c
> v <- popS
> f <- popS
> case f of
> (VFun' env' n f) -> do
> let c'' = put ((n, v):env')
> exec' f c''
> _ -> throw ("VFun function expected " ++ show f ++ " received"))
> exec' (FUN n t1 t2) c = exec' t2 (do -- ! JEREMY WHAT DO YOU THINK
> c
> env <- get
> pushS (VFun' (conve env) n t1))
> exec' (THK t1 t2) c = exec' t2 (do
> c
> env <- get
> pushS (VThunk' (conve env) t1))
> exec' (DO n t1 t2) c = exec' t2 (do
> c
> v1 <- popS
> case v1 of
> (VReturn v2) -> do
> let c' = pushE (n,conv v2)
> exec' t1 c'
> _ -> throw ("VReturn expected in Do, " ++ show v1 ++ " received"))
> exec' (LET n t1 t2) c = exec' t2 (do
> c
> v <- popS
> let c' = pushE (n,v)
> exec' t1 c')
{-top level compiler function-}
> comp1 :: Expr -> Code
> comp1 x = comp1' x HALT
{-compiler function, derived-}
> comp1' :: Expr -> Code -> Code
> --value types
> comp1' (Val n) t = PUSH (VNum n) t
> comp1' (Vars n) t = LOOKUP n t
> comp1' (Thunk e1) t = THK (comp1 e1) t
> comp1' (ThrowE msg) t = FAIL msg
> --computation types
> comp1' (Add e1 e2) t = comp1' e1 (comp1' e2 (ADD t))
> comp1' (Force e1) t = comp1' e1 (FRC t)
> comp1' (Return e1) t = comp1' e1 (RET t)
> comp1' (Do n e1 e2) t = comp1' e1 (DO n (comp1 e2) t)
> comp1' (Let n e1 e2) t= comp1' e1 (LET n (comp1 e2) t)
> comp1' (Fun n e1) t = FUN n (comp1 e1) t
> comp1' (Apply e1 e2) t= comp1' e1 (comp1' e2 (APP t))
> comp1' (CatchE (ThrowE e) h) t = comp1' h t
> comp1' (CatchE x _) t = comp1' x t
{-
Main> defaultHandler (exec' (comp1 test1) initConf)
([VFun' [] "n" (LOOKUP "n" (PUSH (VNum 3) (ADD (RET HALT))))],([],Right ()))
Main> defaultHandler (exec' (comp1 test2) initConf)
([VReturn (VNum 3)],([],Right ()))
Main> defaultHandler (exec' (comp1 test3) initConf)
([VReturn (VNum 1)],([],Right ()))
Main> defaultHandler (exec' (comp1 test4) initConf)
([VReturn (VNum 16)],([("x",VNum 8)],Right ()))
Main> defaultHandler (exec' (comp1 test5) initConf)
([VReturn (VNum 16)],([("x",VNum 8)],Right ()))
-}
> type Sigma = HExc EMsg :+: HState Env :+: HStack ExprValue :+: HVoid
--configuration instance
> instance (HStack ExprValue :<: Sigma,
> HExc EMsg :<: Sigma,
> HState Env :<: Sigma) =>
> Configuration
> Expr (Free Sigma) () (Stack, (Env, Either EMsg ())) where
> eval = eval'
> handle = defaultHandler
-- correct compiler instance
> instance (HStack ExprValue :<: Sigma,
> HExc EMsg :<: Sigma,
> HState Env :<: Sigma) =>
> CorrectCompiler
> Expr Code (Free Sigma) () (Stack, (Env, Either EMsg ())) where
> exec = exec'
> comp = comp1
> comp' = comp1'
-- pretty printer
> pp :: (Show a, Show b) => (a, (b, c)) -> IO ()
> pp (a, (b, c)) = do
> putStrLn ("Stack: " ++ show a)
> putStrLn ("Env: " ++ show b)
{-
Main> defaultHandler (exec' HALT (eval' test3 initConf))
([VReturn (VNum 1)],([],Right ()))
Main> defaultHandler (exec' (comp1 test3) initConf)
([VReturn (VNum 1)],([],Right ()))
-}
-- unsatisfactory as expressions are not compiled proper, and not tail recursive
-- lack of time DNF
-- modified semantic domain with exprs replaced with code
> data ExprValue'
> = VNum' Int -- Integer literal
> | VThunk'' Env' Code' -- thunk value
> | VReturn' ExprValue' -- return value
> | VFun'' Env' Name Code' -- function closure returned
> deriving Show
> type Env' = [(Name, ExprValue')]
> emptyEnv' = []
> type Stack' = [ExprValue']
> emptyStack' = []
> type Conf' = (Stack', Env')
> conv' :: ExprValue -> ExprValue'
> conv' (VNum n) = VNum' n
> conv' (VThunk env e1) = VThunk'' (conve' env) (comp2 e1)
> conv' (VReturn v) = VReturn' (conv' v)
> conv' (VFun env n e1) = VFun'' (conve' env) n (comp2 e1)
> conve' :: Env -> Env'
> conve' = fmap (\(n,v) -> (n, conv' v))
> convs' :: Stack -> Stack'
> convs' = fmap conv'
> execHandler :: Free (HExc EMsg :+: (HState Env' :+: (HStack ExprValue' :+: HVoid))) a
> -> ([ExprValue'], (Env', Either EMsg a))
> execHandler = handler (emptyStack'::Stack') (emptyEnv'::Env')
Main> pp (execHandler (exec2' (comp2 (Let "n" (Val 2) (Vars "n"))) initConf))
Stack: [VNum' 2]
Env: [("n",VNum' 2)]
Main> pp (defaultHandler (eval' (Let "n" (Val 2) (Vars "n")) initConf))
Stack: [VNum 2]
Env: [("n",VNum 2)]
> popS' :: (HStack ExprValue' :<: g)=>
> Free g ExprValue'
> popS' = inject (Lift (PopS Var))
> pushS' :: (HStack ExprValue' :<: g)=>
> ExprValue' -> Free g ()
> pushS' v = inject (Lift (PushS v (Var ())))
> data Code' where
> HALT' :: Code'
> PUSH' :: ExprValue' -> Code' -> Code'
> LOOKUP' :: Name -> Code' -> Code'
> FAIL' :: EMsg -> Code'
> THK' :: Code' -> Code' -> Code'
> ADD' :: Code' -> Code'
> FRC' :: Code' -> Code'
> RET' :: Code' -> Code'
> APP' :: Code' -> Code'
> FUN' :: Name -> Code' -> Code' -> Code'
> DO' :: Name -> Code' -> Code' -> Code'
> LET' :: Name -> Code' -> Code' -> Code'
> deriving Show
> exec2' :: (HStack ExprValue' :<: g, HExc EMsg :<: g, HState Env' :<: g) =>
> Code' ->
> Free g () ->
> Free g ()
> exec2' HALT' c = c
> exec2' (PUSH' v t) c = exec2' t (do {c; pushS' v})
> exec2' (LOOKUP' n t) c = exec2' t (do
> c
> env <- get
> case lookup n env of
> Nothing -> throw ("Unknown Variable " ++ n)
> Just v -> pushS' v)
> exec2' (FAIL' e) c = throw e
> exec2' (ADD' t) c = exec2' t (do
> c
> v1 <- popS'
> v2 <- popS'
> case (v1, v2) of
> ((VNum' n), (VNum' m)) -> pushS' (VNum' (m+n))
> _ -> throw ("VNums Expected in Addition " ++ show v1 ++ " " ++ show v2 ++ " received"))
> exec2' (FRC' t) c = exec2' t (do
> c
> v <- popS'
> case v of
> (VThunk'' env t2) -> do
> let c' = put env
> exec2' t2 c'
> _ -> throw ("VThunk expected in Force, " ++ show v ++ " received"))
> exec2' (RET' t) c = exec2' t (do
> c
> v <- popS'
> pushS' (VReturn' v))
> exec2' (APP' t) c = exec2' t (do
> c
> v <- popS'
> f <- popS'
> case f of
> (VFun'' env' n f) -> do
> let c'' = put ((n,v):env')
> exec2' f c''
> _ -> throw ("VFun function expected " ++ show f ++ " received"))
> exec2' (FUN' n t1 t) c = exec2' t (do
> c
> env <- get
> pushS' (VFun'' env n t1))
> exec2' (THK' t1 t) c = exec2' t (do
> c
> env <- get
> pushS' (VThunk'' env t1))
> exec2' (DO' n t1 t2) c = exec2' t2 (do
> c
> v1 <- popS'
> case v1 of
> (VReturn' v2) -> do
> let c' = pushE (n,v2)
> exec2' t1 c'
> _ -> throw ("VReturn expected in Do, " ++ show v1 ++ " received"))
> exec2' (LET' n t1 t2) c = exec2' t2 (do
> c
> v <- popS'
> let c' = pushE (n,v)
> exec2' t1 c')
> comp2 :: Expr -> Code'
> comp2 x = comp2' x HALT'
> comp2' :: Expr -> Code' -> Code'
> --value types
> comp2' (Val n) t = PUSH' (VNum' n) t
> comp2' (Vars n) t = LOOKUP' n t
> comp2' (Thunk e1) t = THK' (comp2 e1) t
> comp2' (ThrowE msg) t = FAIL' msg
> --computation types
> comp2' (Add e1 e2) t = comp2' e1 (comp2' e2 (ADD' t))
> comp2' (Force e1) t = comp2' e1 (FRC' t)
> comp2' (Return e1) t = comp2' e1 (RET' t)
> comp2' (Do n e1 e2) t = comp2' e1 (DO' n (comp2' e2 HALT') t)
> comp2' (Let n e1 e2) t= comp2' e1 (LET' n (comp2' e2 HALT') t)
> comp2' (Fun n e1) t = FUN' n (comp2 e1) t
> comp2' (Apply e1 e2) t= comp2' e1 (comp2' e2 (APP' t))
> comp2' (CatchE (ThrowE e) h) t = comp2' h t
> comp2' (CatchE x h) t = comp2' x t