I've been given the following Data types:
data Aexp = N Integer | V Var | Add Aexp Aexp | Mult Aexp Aexp | Sub Aexp Aexp
data Bexp = Bcon Bool | Eq Aexp Aexp | Le Aexp Aexp | Neg Bexp | And Bexp Bexp
data Stm = Ass Var Aexp | Skip | Comp Stm Stm | If Bexp Stm Stm | While Bexp Stm | Block DecV DecP Stm | Call Pname
For Arithmetic expressions, Boolean expressions and Statements respectively.
I have been asked to return a Stm representing the following program:
x:=5;
y:=1;
while ¬(x=1) do
y:=y*x;
x:=x-1
So far I have the following :
p :: Stm
p = (Ass x 5) (Ass y 1) (While (Neg (Eq x 1)) Ass x (Sub x 1) Ass y (Mult y x))
I did this simply by writing out the program on paper and then drawing a syntax tree from it. Firstly I'm not sure if this is actually correct as I don't really know how to prove it. And secondy when I compile I get lots of errors like:
denotational.hs:122:10: Not in scope: `x'
denotational.hs:122:20: Not in scope: `y'
denotational.hs:122:41: Not in scope: `x'
denotational.hs:122:51: Not in scope: `x'
denotational.hs:122:58: Not in scope: `x'
denotational.hs:122:67: Not in scope: `y'
denotational.hs:122:75: Not in scope: `y'
denotational.hs:122:77: Not in scope: `x'
Any help with this would be greatly appreciated. Thanks
xandyaren't defined anywhere, so you can't use them in you definition ofp. You probably want to doAss "x" 5etc., assuming thattype Var = String.type Var = Stringhowever when I tried that with all the variables I get all kinds of errors. For exampleThe function Ass is applied to four arguments, but its type Var -> Aexp -> Stm has only twoorThe function While' is applied to 7 arguments, but its type Bexp -> Stm -> Stm has only twoI think I might be constructing the actual statement incorrectly compared to the data type I'm given. But I can't really see how.(Ass x 5)to two arguments:(Ass y 1)and(While (Neg (Eq x 1)) Ass x (Sub x 1) Ass y (Mult y x)). Since you've also already appliedAsstoxand5, the compiler complains that you've given it a total of four arguments. There is a similar problem in the call toWhile.