-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathEvaluator.hs
69 lines (52 loc) · 2.97 KB
/
Evaluator.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
-- @author: Joanna Biega
-- machine evaluation module
module Evaluator where
import Prelude hiding (lex)
import System.Random
import AST
-------------------------------------------------------------------------------------------------------------
getStateTerm :: StateNumber -> Character -> Term
getStateTerm state_num [] = TmDecision TmCurrent "No such state number"
getStateTerm state_num (x:xs) = if elem state_num (fst x) then snd x else getStateTerm state_num xs
runMachine :: StateNumber -> Character -> Env -> (Term, Cost)
runMachine state_num character env = let t = getStateTerm state_num character
in let (term, cost) = eval t env 0 in
case term of
TmDecision TmCurrent s -> (TmDecision (TmState state_num) s, cost)
_ -> (term, cost)
-------------------------------------------------------------------------------------------------------------
eval :: Term -> Env -> Cost -> (Term, Cost)
eval (TmIf cond tt elifs tf) env cost = case evalCondition cond env cost of
(TmTrue, c) -> eval tt env c
(TmFalse, c) -> case elifs of
[] -> eval tf env c
(cnd, term):xs -> eval (TmIf cnd term xs tf) env c
eval (TmCase (TmVar x) arms def) env cost = case lookup x env of
Just val -> let t = chooseCase val arms def in eval t env (cost+11.5)
Nothing -> eval def env (cost+11.5)
eval (TmDecision TmCurrent s) env cost = (TmDecision TmCurrent s, cost + 3)
eval (TmDecision t s) env cost = (TmDecision t s, cost + 4)
-------------------------------------------------------------------------------------------------------------
chooseCase val [] def = def
chooseCase val ((vs, t):xs) def = if elem val vs then t
else chooseCase val xs def
-------------------------------------------------------------------------------------------------------------
evalCondition :: Condition -> Env -> Cost -> (Condition, Cost)
evalCondition (TmEquals (TmVar x) val _) env cost = case lookup x env of
Just v -> if v == val then (TmTrue, cost+6.5) else (TmFalse, cost+6.5)
Nothing -> (TmFalse, cost+6.5)
evalCondition (TmAnd [] _) env cost = (TmTrue, cost)
evalCondition (TmAnd (x:xs) _) env cost = let (cond, c) = evalCondition x env cost in if cond == TmTrue
then evalCondition (TmAnd xs 0) env c
else (TmFalse, c)
evalCondition (TmOr [] _) env cost = (TmFalse, cost)
evalCondition (TmOr (x:xs) _) env cost = let (cond, c) = evalCondition x env cost in if cond == TmTrue
then (TmTrue, c)
else evalCondition (TmOr xs 0) env c
evalCondition TmTrue _ cost = (TmTrue, cost)
evalCondition TmFalse _ cost = (TmFalse, cost)
--------------------------------------------------------------------------------------------------------------
randomEnv :: Character -> IO Env
randomEnv char = do
let vv = varsVals char
mapM (\(vr, vls) -> randomRIO (0, length vls - 1) >>= \ridx -> return (vr, vls !! ridx)) vv