- #1
JamesBwoii
- 74
- 0
Hi, I need to implement a mini imperative language in Haskell but I'm struggling to get my head around it.
I need to implement an increment and decrement function like ++ and -- in other languages.
We've been given some skeleton code which we need to complete in order to run a factorial function using the decrement operator.
Here's the skeleton code:
Here's the factorial function which it needs to run:
This is mainly to teach me about the semantics and as such I've been given nothing the following definitions:
A[v++](s)=(t,n) where t=s[v→n+1] and n=s(v)
A[v−−](s)=(t,n) where t=s[v→n−1] and n=s(v)
This is my attempt so far, but I'm struggling the with Decr and Incr functions and I'm not sure if the rest of what I have done is correct.
Thanks for any help! :D
I need to implement an increment and decrement function like ++ and -- in other languages.
We've been given some skeleton code which we need to complete in order to run a factorial function using the decrement operator.
Here's the skeleton code:
Code:
data Aexp = Num Integer
| Var Variable
| Aexp :+: Aexp
| Aexp :-: Aexp
| Aexp :*: Aexp
evalA (Num n) s = undefined
evalA (Var v) s = undefined
evalA (a :+: b) s = undefined
evalA (a :*: b) s = undefined
evalA (a :-: b) s = undefined
Here's the factorial function which it needs to run:
Code:
factorial :: Comm
factorial = ("y" :=: Num 1) :>:
While (Num 1 :<=: Var "x")
("y" :=: (Var "y" :*: Decr "x"))
runFactorial :: Integer -> Integer
runFactorial i = get "y" s
where
s = evalC factorial (set "x" i empty)
This is mainly to teach me about the semantics and as such I've been given nothing the following definitions:
A[v++](s)=(t,n) where t=s[v→n+1] and n=s(v)
A[v−−](s)=(t,n) where t=s[v→n−1] and n=s(v)
This is my attempt so far, but I'm struggling the with Decr and Incr functions and I'm not sure if the rest of what I have done is correct.
Code:
data Aexp = Num Integer
| Var Variable
| Aexp :+: Aexp
| Aexp :-: Aexp
| Aexp :*: Aexp
| Incr Variable
| Decr VariableevalA :: Aexp -> State -> (State, Integer)
evalA (Num n) s = (s, n)
evalA (Var v) s = (s, v)
evalA (Incr v) s = undefined
evalA (Decr v) s = undefined
evalA (a :+: b) s0 = let (s1, na) = evalA a s0; (s2, nb) = evalA b s1 in (s2, na + nb)
evalA (a :*: b) s0 = let (s1, na) = evalA a s0; (s2, nb) = evalA b s1 in (s2, na * nb)
evalA (a :-: b) s0 = let (s1, na) = evalA a s0; (s2, nb) = evalA b s1 in (s2, na - nb)
Thanks for any help! :D