-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathPure.hs
55 lines (40 loc) · 1.01 KB
/
Pure.hs
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
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# OPTIONS_GHC
-fplugin BinderAnn.Pure
-fplugin-opt BinderAnn.Pure:infix=@@
#-}
module Pure where
import Control.Monad.Writer
import Control.Monad.Identity
import BinderAnn.Pure
data Exp =
Val Int
| Add Exp Exp
| Ann Exp SrcInfo
deriving Show
type Eval = WriterT [SrcInfo] Identity
instance Annotated Exp where
annotate = Ann
runEval :: Eval Exp -> (Int, [SrcInfo])
runEval e = runIdentity (runWriterT (eval =<< e))
runEval' :: Eval Exp -> Exp
runEval' = fst . runIdentity . runWriterT
eval :: Exp -> Eval Int
eval (Val n) = return n
eval (Add x y) = liftM2 (+) (eval x) (eval y)
eval (Ann x a) = tell [a] >> eval x
val :: Int -> Eval Exp
val n = return (Val n)
(|+|) :: Exp -> Exp -> Eval Exp
x |+| y = return (Add x y)
test1 = runEval @@ do
(x, y) <- (,) <$> val 10 <*> val 5
z <- return False
w <- (if z then val 1 else val 2)
s1 <- x |+| y
s1 |+| w
tests :: IO ()
tests = do
putStrLn "test1:"
print test1