Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Code in F#. Implement the function desugarChoose : expr -> expr, which should convert the given expression to an equivalent expression that does not use

Code in F#.

Implement the function desugarChoose : expr -> expr, which should convert the given expression to an equivalent expression that does not use Choose. You will need to use Try and Fail. Be careful to ensure that you desugar all subexpressions.

let rec desugarChoose (e : expr) : expr =

failwith "Not implemented"

Here are the test cases to try:

// > eval (desugarChoose (Choose [Fail; Fail; Fail; Num 0])) [];;

// val it: value = VNum 0

// > eval (desugarChoose (Choose [Fail; Fail; Fail; Fail])) [];;

// val it: value = VFail

// > eval (desugarChoose (Choose [Choose [Fail; Num 0]; Choose [Fail]])) [];;

// val it: value = VNum 0

// > eval (desugarChoose (Choose [Choose [Fail]; Choose [Fail; Num 0]; Choose [Fail]])) [];;

// val it: value = VNum 0

// > eval (desugarChoose (Choose [ Fail; Or (Choose [ True ; Var "x" ], Num 0)])) [];;

// val it: value = VBool true

let e2 =

desugarChoose (Plus (Var "y",

Choose

[ If (LessThan (Var "x", Num 0), Fail, Var "x")

; If (LessThan (Plus (Var "x", Var "y"), Num 0), Fail, Plus (Var "x", Var "y"))

; Choose [ Var "y" ]]))

// > eval e2 ["x", 10; "y", 20]

// val it: value = VNum 30

// > eval e2 ["x", -15; "y", 20]

// val it: value = VNum 25

// > eval e2 ["x", -15; "y", -20]

// val it: value = VNum -40

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

50 Tips And Tricks For MongoDB Developers Get The Most Out Of Your Database

Authors: Kristina Chodorow

1st Edition

1449304613, 978-1449304614

More Books

Students also viewed these Databases questions