Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need to implement the IFELSE type so that I get the results correctly as seen in the picture below. Here is my code below

I need to implement the IFELSE type so that I get the results correctly as seen in the picture below. Here is my code below in Haskell:

RULES

-- You can't modify any functions

--LDI: loads one integer to the stack

--IFELSE :

- rule for the IFELSE: If the value on top of the stack is true, then run the first program, else run the second program

-- Since we do not know which branch is executed we will need to verify that there is enough stack to execute either branch on the current stack contents.

-- If either Prog1 or Prog2 produces a rank error then the entire IFELSE produces an error.

-- If the IFELSE command does not produce a rank error then set the rank of the current stack used for subsequent command to the min {rank(Prog1) , rank (Prog2)}. --For example, p2 = [IFELSE [ADD] [LDI 2, DUP] ] ran with stack s2 = [B True, I 5] of rank 2 would result in a rank error.

--However, if p2 ran with stack s3 = [B False, I 10] the result would be [I 2, I 2,I 10].

--Since we dont know which branch the IFELSE statement will take we will -------assign a Rank Error if the stack is rank 2.

CODE:

type Prog = [Cmd] data Cmd = LDI Int | ADD | MULT | DUP | DEC | SWAP | POP Int | IFELSE Prog Prog | LDB Bool | LEQ deriving Show data Val = I Int | B Bool deriving Show

type Stack = [Val]

data Result = A Stack | RankError | TypeError deriving Show

type D = Stack -> Stack type Rank = Int type CmdRank = (Int, Int)

-- Define a function rankP that computes the rank of a program -- when ran with a stack of rank r. -- Maybe data type is used to capture rank error. rankP :: Prog -> Rank -> Maybe Rank rankP [] s' = Just s' rankP (x:cs) s | s

run :: Prog -> Stack -> Result run (x:cs) s = case rankP [x] (length s) of Nothing -> RankError Just r -> case semCmd x s of Just s' -> if length s' /= r then TypeError else A s' Nothing -> TypeError

semCmd :: Cmd -> Stack -> Maybe Stack -- loads integer onto the stack semCmd (LDI x ) s = Just (I x:s)

-- loads boolean parameter onto the stack semCmd (LDB b ) s = Just (B b:s)

-- add integers onto the stack semCmd ADD (I x:I y:xs) = Just (I (x+y):xs)

-- multiplies integer --semCmd MULT (I x:I y:xs) = Just (I (x*y):xs)

-- places a copy of the stack's topmost element on the stack semCmd DUP (x:xs) = Just (x:x:xs)

-- removes the top integer from the stack. -- If top is greater than the second top, then the true is pushed on the stack --semCmd LEQ (I x: I y:xs) = Just (B (x

-- removes the top of the stack and if the value is true, --then run the first program, else run the secound program. --semCmd (IFELSE m n) (B x:xs) = run (if x then m else n) xs semCmd (IFELSE p q) (B True:s) = case rankP p (length s) of Just r -> case run p s of A s' -> Just s' _ -> Nothing _ -> Nothing semCmd (IFELSE p q) (B False:s) = case rankP q (length s) of Just r -> case run q s of A s' -> Just s' _ -> Nothing _ -> Nothing semCmd (IFELSE _ _) _ = Nothing semCmd _ _ = Nothing

--rank(n,m) = where n is what is being taken off the stack and m is what is being inserted in the stack.

-- For example:

-- rankC (LDI _) = (0, 1) since LDI adds one value to the stack.

rankC::Cmd -> CmdRank rankC (LDI _) = (0,1) rankC (LDB _) = (0,1) rankC LEQ = (2,1) rankC ADD = (2,1) rankC MULT = (2,1) rankC DUP = (1,2) rankC DEC = (1,1) rankC SWAP = (2,2) rankC (POP k) = (k,0) rankC (IFELSE p q) = (0,0)

These are the outputs that should generate:

image text in transcribed

image text in transcribedimage text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

Explain the Neolithic age compared to the paleolithic age ?

Answered: 1 week ago

Question

What is loss of bone density and strength as ?

Answered: 1 week ago

Question

What does Processing of an OLAP Cube accomplish?

Answered: 1 week ago