Define a function that takes an environment

Assignment Help Programming Languages
Reference no: EM13850776

QUESTION 1

Define a function that takes an environment and an identifier, and indicates whether or not the identifier has a value associated with in the environment. In other words, (hasBinding env id) should return False exactly if applyEnv env id raises an error.

hasBinding :: Env -> Id -> Bool

hasBinding = undefined

QUESTION 2

The Exp datatype (and parser) includes a "cond" constructor, which is a generalized if-then-else ("cond" for "conditional"). E.g. the
-- language expression
-- cond iszero(x) ==> +(x,1)
-- iszero(pred(x)) ==> 2
-- iszero(pred(pred(x) ==> 3
-- true ==> 4
-- end
-- has 4 clauses, each of which is a test followed by an expression to evaluate in the case the test evaluates to True.

To evaluate a "cond", evaluate the tests in order, and when one is found whose value is not zero, return the value of the corresponding result expression. If no true test is found, return 0 for the value of the cond expression.

In the above example, if x is bound to 2, the first 2 tests are false and the third is true, so the result of the whole cond expression is 3. Modify the interpreter to handle this new language feature; you'll need to modify the definition of evalExp above. Ignore evalExp'.

Question 3 Write a function boundIds that gives a list of all ids bound in the environment. Use this function and traceShow (imported from Debug.Trace) to print out a new line of currently-bound ids each time a function is called. Primitive operations don't count: only modify the evalExp clause for App. See the examples section for more.

boundIds :: Env -> [Id]
boundIds = undefined

Question 4

The Exp datatype (and parser) includes an "slet" constructor that is syntactically identical to "let" except for using the keyword "slet". The difference is in how the let bindings are evaluated.

The regular "let" is *parallel* binding: the right-hand sides are all evaluated in the same environment. The "slet" is sequential:

each let binding clause is processed in turn, and the binding it creates is added to the environment for evaluating the next binding.

For example, "let x=3 y=+(x,1) in y" is an error since +(x,1) is evaluated in the empty environment; "slet x=3 y=+(x,1) in y"evaluates to

4. Complete the evalExp clause(s) for SLet above.

Question 5

-- The "primed" version of the evaluator (run', evalExp' etc) is identical to the regular evaluator except for line with "***" beside it: here evalExp' uses env instead of closureEnv. Give an example where run and run' produce different results.

-- run q5Eg /= run' q5Eg
q5Eg :: String
q5Eg = undefined

-- Examples / Test Data

-- Q1

bogusFrame idSentence =

Frame ids (map (const $ Int 0) ids) where ids = words idSentence

envExample =

foldr ExtendedEnv EmptyEnv

$ map bogusFrame ["x y z", "u v", "s u x v", "a b c"]

-- Should be true.

q1Test =

map (hasBinding envExample) (words "x u g a i")

==
[True, True, False, True, False]

-- Q2

condeg0 =
"let x = 2 in \
\cond iszero(x) ==> +(x,1) \
\ iszero(pred(x)) ==> 2 \
\ iszero(pred(pred(x))) ==> 3 \
\ true ==> 4 \
\end"

condeg1 =
"let g = proc(x) cond iszero(x) ==> 0 true ==> 1 end in (g (g 3))"

condeg2 =
"let g = proc(x) cond iszero(x) ==> 0 false ==> 1 end in (g (g 3))"

-- Should be true.
q2Test =
map run [condeg0, condeg1, condeg2]
==
[Int 3, Int 1, Int 0]


-- Q3

q3eg =
" let f=proc(x) +(x,3) in \
\ let g = proc(h,y) succ((h (f y))) in \
\ let fact = \
\ letrec f(n) = if iszero(n) then 1 else *(n,(f pred(n))) \
\ in f \
\ in \
\ (g fact 17)"

-- Output should be something like:

-- ["fact","g","f"]
-- ["h","y","f"]
-- ["h","y","f"]
-- ["n","f","g","f"]
-- ["n","f","g","f"]
-- ["n","f","g","f"]
-- ["n","f","g","f"]
-- ["n","f","g","f"]
-- ["n","f","g","f"]
-- ["n","f","g","f"]
-- ["n","f","g","f"]
-- ["n","f","g","f"]
-- ["n","f","g","f"]
-- ["n","f","g","f"]
-- ["n","f","g","f"]
-- ["n","f","g","f"]
-- ["n","f","g","f"]
-- ["n","f","g","f"]
-- ["n","f","g","f"]
-- ["n","f","g","f"]
-- ["n","f","g","f"]
-- ["n","f","g","f"]
-- ["n","f","g","f"]
-- Int 2432902008176640001


-- Q4

slet0 = "slet x=3 y=+(x,1) in y"
slet1 = "let x=3 y=4 in slet z=2 f=proc() +(x,z) in (f)"
slet2 = "let x=2 y=4 in slet x=succ(x) u=x z=y in +(x,+(u,z))"

-- should be true
q4Test =
map run [slet0, slet1, slet2]
==
[Int 4, Int 5, Int 10]


-- Miscellaneous examples just to illustrate the language.

eg0 = "+(2,3)"
eg1 = "+(+(2,3), 4)"
eg2 = "let x=17 y=2 in *(x,y)"
eg3 = "let x=0 y=1 in let x=2 in x"
eg4 = "let x=-(3,3) in if iszero(x) then 17 else x"
eg5 = "let x=0 in +(let y=2 z=3 in +(y,z), x)"
eg6 = "let x=1 in let f = proc (y) +(x,y) in let x=2 in (f 17)"

eg7 =
"letrec even(x) = if iszero(x) then true else (odd pred(x)) \
\odd(x) = if iszero(x) then false else (even pred(x)) \
\in (odd 12)"

eg8 =
"let x=1 in \
\let g = proc (m) succ(m) in \
\letrec f(n) = if iszero(n) then x else (g (f pred(n))) in \
\(f 1)"


-- evaluates to true
miscTest =
map run [eg0, eg1, eg2, eg3, eg4, eg5, eg6, eg7, eg8]
==
[Int 5,Int 9,Int 34,Int 2,Int 17,Int 5,Int 18,Bool False,Int 2]


Attachment:- 1.zip

Reference no: EM13850776

Questions Cloud

Credit card-introductory interest rate : You have received a letter from a credit card company offering you a credit card with a limit of $5,000 at an introductory interest rate of 5%. What additional information do you need in order to make a decision whether to accept this offer? List spe..
Reflect and examine your looking-glass self : REFLECT and EXAMINE your Looking-Glass Self as it was in high school and then in adulthood. How did it change during the transition from high school to adulthood? IDENTIFY the people that influenced, or caused, you to see yourself differently.
Major trends that profitability ratios exhibit : Describe the major trends that profitability ratios exhibit, and provide an opinion on what this means to the company. Describe how this company is doing relative to its industry (compare your company’s ratios to the industry’s ratios). This is for P..
How might this impact his or her academic career : If a student is caught in this situation, how might this impact his or her academic career, professional career, or reputation in the local community
Define a function that takes an environment : Define a function that takes an environment and an identifier, and indicates whether or not the identifier has a value associated with in the environment.
Using the normalization process and rules : The 1NF table(s) - and an explanation of why each table(s), is in 1NF - using normal form definitions.
Major trends that asset management ratios exhibit : Describe the major trends that asset management ratios exhibit, and provide an opinion on what this means to the company. Describe how this company is doing relative to its industry (compare your company’s ratios to the industry’s ratios). This is fo..
What is your estimate of the stocks current price : A company currently pays a dividend of $3 per share (D0 = $3). It is estimated that the company's dividend will grow at a rate of 16% per year for the next 2 years, then at a constant rate of 7% thereafter. The company's stock has a beta of 1.05, the..
What is the projected effective gross income : You intend to develop a rental apartment building that has 100 units. It is in a good market and you will immediately fill it up, though with people moving in and out, and fixing up apartments between tenants, you have a typical vacancy rate of 5%. W..

Reviews

Write a Review

Programming Languages Questions & Answers

  Program to allot new value to first element of array

Write the single statement which assigns new value to first element of array. This new value must be equal to twice value stored in the last element of the array.

  Html5 assignment i need an assignment for html5 containing

i need an assignment for html5 containing 2 parts1 3 pages html5 website2 report more important than the website

  Write code using while loop for data validation

Write code or pseudo code using while loops to do Data Validation for following user prompts:input positive # input negative # input number between 1 and 100, inclusive.

  Write program using array to show fifo queue

Write program using any language which uses the array to demonstrate simple FIFO queue with 10 job entering the queue and 5 jobs removed from the queue by the server.

  Create e-r diagram for sell and buy of antiques

Create E-R diagram for following situation (state any assumptions you believe you have to make to develop diagram): XYZ Antiques buys and sells one-of-a kind antiques of all kinds

  Design function to accept two integer values

Design function named max that accepts two integer values as arguments and returns the value that is the greater of the two. for example if 7 and 12 are passed as arguments to the function the function should return 12.

  Write vl program to convert leagues-number of nautical miles

Write a VL program which will convert 20,000 leagues to number of nautical miles. Your program requires no input elements, just elements to compute result and output how many nautical miles is equivalent to 20,000 leagues.

  Write method that accepts as parameter reference

Write a method maxVal that accepts as parameter the reference to the head node of a linked list of integers. The method should return the largest value in the list.

  About html

Hyper Text Markup Language.

  You will have to override some of the step-by-step procedure

Actually the author has not asked you to develop any new page here except that you will need to apply your external style sheet in your Index and Yurts pages done in Assignment 1A.

  London underground fire at the kings cross underground

london underground fire at the kings cross underground station on 18 november 1987 a wooden escalator caught fire

  Program to output circle-s radius-diameter-circumference

Write a program to prompt the user to enter center and a point on circle. Program must then output circle's radius, diameter, circumference, and area.

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd