forked from utgheith/p3
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSmall.hs
More file actions
193 lines (172 loc) · 4.71 KB
/
Copy pathSmall.hs
File metadata and controls
193 lines (172 loc) · 4.71 KB
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
{-# LANGUAGE ConstrainedClassMethods #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
module Small (reduceFully, Machine (..), Result (..), Env) where
import qualified Control.Monad.State as S
import Data.Either
import Debug.Trace (trace)
import Term (BinaryOp (..), Term (..))
import Value (Value (..), valueToInt)
----- The Machine type class -----
-- The micro-ops that a machine must support
-- Allow an implementation to define its own semantics
class Machine m where
type V m -- The value type for this machine
-- Uses associated an associated type family for the value type
-- This requires the TypeFamilies extension
-- The way you read the type signature is:
-- for any type m that is an instance of Machine, there is an associated type (V m)
-- Get and set variables
getVar :: String -> Env m
setVar :: String -> V m -> Env m
-- I/O
inputVal :: Env m
outputVal :: V m -> Env m
-- Arithmetic and control
addVal :: V m -> V m -> Env m
subVal :: V m -> V m -> Env m
mulVal :: V m -> V m -> Env m
divVal :: V m -> V m -> Env m
modVal :: V m -> V m -> Env m
-- Comparison operations (operate on integers, return booleans)
ltVal :: V m -> V m -> Env m
gtVal :: V m -> V m -> Env m
lteVal :: V m -> V m -> Env m
gteVal :: V m -> V m -> Env m
eqVal :: V m -> V m -> Env m
neqVal :: V m -> V m -> Env m
-- Logical operations (operate on booleans)
andVal :: V m -> V m -> Env m
orVal :: V m -> V m -> Env m
notVal :: V m -> Env m
-- Control flow - selectValue uses boolean semantics
selectValue :: V m -> Env m -> Env m -> Env m
----- The Result type -----
data Result a
= Happy a -- produced an answer
| Continue Term -- need to keep going
| Sad String -- error
deriving (Eq, Show)
----- The Env monad -----
-- abstract semantics that glue micro-ops together
type Env m = S.State m (Result (V m))
premise :: Env m -> (Term -> Term) -> (V m -> Env m) -> Env m
premise e l r = do
v <- e
case v of
Continue t' -> return $ Continue (l t')
Happy n -> r n
Sad _ -> return v
------ Small-step reduction ------
reduce_ :: (Machine m, Show m, V m ~ Value) => Term -> Env m
reduce_ (Literal n) =
return $ Happy $ IntVal n
reduce_ (StringLiteral s) =
return $ Happy $ StringVal s
reduce_ (Var x) =
getVar x
reduce_ (Let x t) = do
premise
(reduce t)
(Let x)
(setVar x)
reduce_ (Seq t1 t2) = do
premise
(reduce t1)
(`Seq` t2)
(\_ -> return $ Continue t2)
reduce_ (If cond tThen tElse) = do
premise
(reduce cond)
(\cond' -> If cond' tThen tElse)
(\v -> selectValue v (return $ Continue tThen) (return $ Continue tElse))
reduce_ w@(While cond body) =
return $ Continue (If cond (Seq body w) Skip)
reduce_ (Read x) =
premise
inputVal
id
(setVar x)
reduce_ (Write t) = do
premise
(reduce t)
Write
outputVal
reduce_ Skip =
return $ Happy (IntVal 0)
reduce_ (BinaryOps op t1 t2) =
premise
(reduce t1)
(\t1' -> BinaryOps op t1' t2)
( \v1 ->
premise
(reduce t2)
(BinaryOps op (Literal $ fromRight (-1) (valueToInt v1)))
(applyBinaryOp op v1)
)
where
applyBinaryOp Add = addVal
applyBinaryOp Sub = subVal
applyBinaryOp Mul = mulVal
applyBinaryOp Div = divVal
applyBinaryOp Mod = modVal
reduce_ (BoolLit b) =
return $ Happy $ BoolVal b
reduce_ (Lt t1 t2) =
premise
(reduce t1)
(`Lt` t2)
(premise (reduce t2) (const Skip) . ltVal)
reduce_ (Gt t1 t2) =
premise
(reduce t1)
(`Gt` t2)
(premise (reduce t2) (const Skip) . gtVal)
reduce_ (Lte t1 t2) =
premise
(reduce t1)
(`Lte` t2)
(premise (reduce t2) (const Skip) . lteVal)
reduce_ (Gte t1 t2) =
premise
(reduce t1)
(`Gte` t2)
(premise (reduce t2) (const Skip) . gteVal)
reduce_ (Eq t1 t2) =
premise
(reduce t1)
(`Eq` t2)
(premise (reduce t2) (const Skip) . eqVal)
reduce_ (Neq t1 t2) =
premise
(reduce t1)
(`Neq` t2)
(premise (reduce t2) (const Skip) . neqVal)
reduce_ (And t1 t2) =
premise
(reduce t1)
(`And` t2)
(premise (reduce t2) (const Skip) . andVal)
reduce_ (Or t1 t2) =
premise
(reduce t1)
(`Or` t2)
(premise (reduce t2) (const Skip) . orVal)
reduce_ (Not t) =
premise
(reduce t)
Not
notVal
reduce :: (Machine m, Show m, V m ~ Value) => Term -> Env m
reduce t = do
e <- S.get
trace ("Simulating: " ++ show t) () `seq`
trace (" Machine: " ++ show e) () `seq`
reduce_ t
reduceFully :: (Machine m, Show m, V m ~ Value) => Term -> m -> (Either String (V m), m)
reduceFully term machine =
case S.runState (reduce term) machine of
(Sad msg, m) -> (Left msg, m)
(Continue t, m) -> reduceFully t m
(Happy n, m) -> (Right n, m)