data Var = X | Y | Z deriving (Eq, Show) 

{-
type State = Var -> Integer

lkp :: Var -> State -> Integer
lkp x s = s x

update :: Var -> Integer -> State -> State
update x z s = \ y -> if x == y then z else s y

init :: State
init = \ _ -> 0
-}

type State = [(Var, Integer)]

lkp :: Var -> State -> Integer
lkp x [] = 0
lkp x ((y,z):s) = if x == y then z else lkp x s

upd :: Var -> Integer -> State -> State
--upd x z s = (x,z):s 
upd x z [] = [(x,z)]
upd x z ((y,w):s) = if x == y then (x,z):s
                       else (y,w):upd x z s

init :: State
init = []


data AExp = N Integer | V Var 
          | AExp :+ AExp | AExp :- AExp | AExp :* AExp

aexp :: AExp -> State -> Integer
aexp (N z) _      = z
aexp (V x) s      = lkp x s
aexp (a0 :+ a1) s = aexp a0 s + aexp a1 s
aexp (a0 :- a1) s = aexp a0 s - aexp a1 s
aexp (a0 :* a1) s = aexp a0 s * aexp a1 s


data BExp = TT | FF | AExp :== AExp | AExp :<= AExp 
          | Not BExp | BExp :&& BExp | BExp :|| BExp

bexp :: BExp -> State -> Bool
bexp TT _ = True
bexp FF _ = False
bexp (a0 :== a1) s = aexp a0 s == aexp a1 s
bexp (a0 :<= a1) s = aexp a0 s <= aexp a1 s
bexp (Not b) s = not (bexp b s)
bexp (a0 :&& a1) s = bexp a0 s && bexp a1 s
bexp (a0 :|| a1) s = bexp a0 s || bexp a1 s


data Stmt = Skip | Stmt :\ Stmt 
          | Var := AExp
          | If BExp Stmt Stmt
          | While BExp Stmt

stmt :: Stmt -> State -> State
stmt Skip s = s
stmt (stm0 :\ stm1) s = s' where
                          s'' = stmt stm0 s           
                          s'  = stmt stm1 s'' 
-- stmt (stm0 :\ stm1) s = stmt stm1 (stmt stm0 s)
stmt (x := a) s = upd x z s where
                          z = aexp a s
stmt (If b stm0 stm1) s = 
    if bexp b s then 
      stmt stm0 s
    else stmt stm1 s
stmt (While b stm0) s = 
    if bexp b s then 
      stmt (While b stm0) (stmt stm0 s) 
    else s

fac :: Stmt 
fac = (Y := N 1) :\ 
      (While (N 1 :<= V X)
        ((Y := (V Y :* V X)) :\
         (X := (V X :- N 1))  
        )
      )

-- abstract machines

type Config = (Code, Stack, State)

type Stack = [Either Integer Bool]

data Instr = Push Integer | Add | Sub | Mul
           | PushTrue | PushFalse | Eq | Le | Neg | And | Or
           | Fetch Var | Store Var
           | Branch Code Code
           | Loop Code Code  deriving Show

type Code = [Instr]


compAExp :: AExp -> Code
compAExp (N z) = [Push  z]
compAExp (V x) = [Fetch x]
compAExp (a0 :+ a1) = compAExp a1 ++ compAExp a0 ++ [Add]
compAExp (a0 :- a1) = compAExp a1 ++ compAExp a0 ++ [Sub]
compAExp (a0 :* a1) = compAExp a1 ++ compAExp a0 ++ [Mul]


compBExp :: BExp -> Code
compBExp (TT) = [PushTrue]
compBExp (FF) = [PushFalse]
compBExp (a0 :== a1) = compAExp a1 ++ compAExp a0 ++ [Eq]
compBExp (a0 :<= a1) = compAExp a1 ++ compAExp a0 ++ [Le]
compBExp (Not b) = compBExp b ++ [Neg]
compBExp (b0 :&& b1) = compBExp b1 ++ compBExp b0 ++ [And]
compBExp (b0 :|| b1) = compBExp b1 ++ compBExp b0 ++ [Or]


compStmt :: Stmt -> Code
compStmt Skip = []
compStmt (stm0 :\ stm1) = compStmt stm0 ++ compStmt stm1
compStmt (x := a) = compAExp a ++ [Store x]
compStmt (If b stm0 stm1) 
   = compBExp b ++ [Branch (compStmt stm0) (compStmt stm1)] 
compStmt (While b stm0)
   = [Loop (compBExp b) (compStmt stm0)]


makeStep :: Config -> Config 
makeStep (Push z : c, e, s) = (c, Left z : e, s)
makeStep (Add : c, Left z0 : Left z1 : e, s)
                            = (c, Left (z0 + z1) : e, s)
makeStep (Sub : c, Left z0 : Left z1 : e, s)
                            = (c, Left (z0 - z1) : e, s)
makeStep (Mul : c, Left z0 : Left z1 : e, s)
                            = (c, Left (z0 * z1) : e, s)
makeStep (PushTrue  : c, e, s) = (c, Right True  : e, s)
makeStep (PushFalse : c, e, s) = (c, Right False : e, s)
makeStep (Eq : c, Left z0 : Left z1 : e, s)
                            = (c, Right (z0 == z1) : e, s)
makeStep (Le : c, Left z0 : Left z1 : e, s)
                            = (c, Right (z0 <= z1) : e, s)
makeStep (Neg : c, Right z : e, s)
                            = (c, Right (not z) : e, s)
makeStep (And : c, Right z0 : Right z1 : e, s)
                            = (c, Right (z0 && z1) : e, s)
makeStep (Or  : c, Right z0 : Right z1 : e, s)
                            = (c, Right (z0 || z1) : e, s)
makeStep (Fetch x : c, e, s) = (c, Left (lkp x s) : e, s)
makeStep (Store x : c, Left z : e, s) = (c, e, upd x z s)
makeStep (Branch c0 c1 : c, Right True : e, s) 
                             = (c0 ++ c, e, s)
makeStep (Branch c0 c1 : c, Right False : e, s)
                             = (c1 ++ c, e, s)
makeStep (Loop c0 c1 : c, e, s) 
  = (c0 ++ (Branch (c1 ++ [Loop c0 c1]) [] : c), e, s)
 

run :: Config -> Config
run cfg@([] , _, _) = cfg 
run cfg@(_:_, _, _) = run (makeStep cfg)

runSlow :: Config -> [Config]
runSlow cfg@([], _, _) = [cfg] 
runSlow cfg@(_:_, _, _) = cfg : runSlow (makeStep cfg)


                                 
