Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

(I need help with problem 2. It's in Scalar/Scala, and I can't pass all the test cases. It would be greatly appreciated if you could

(I need help with problem 2. It's in Scalar/Scala, and I can't pass all the test cases. It would be greatly appreciated if you could show me how to do this problem and explain it. I'll give problem one so you can see everything, and just in case problem 2 build off 1. Thank you)

image text in transcribed

sealed trait Expr case class Const(d: Double) extends Expr case class Ident(s: String) extends Expr case class Plus(e1: Expr, e2: Expr) extends Expr case class Mult(e1: Expr, e2: Expr) extends Expr case class Let(id: String, e1: Expr, e2: Expr) extends Expr case class MultiLet(id: List[String], eList: List[Expr], e2: Expr) extends Expr

image text in transcribed

sealed trait Value case class NumValue(f: Double) extends Value case object Error extends Value /* -- Do not return Error -- simply throw an new IllegalArgumentException whenever you encounter an erroneous case --*/

type Environment = Map[String, Value]

def evalExpr(e: Expr, env: Environment): Value = { e match { case Const(f) => NumValue(f) case Ident(x) => { if (env.contains(x)) { env(x) } else { throw new IllegalArgumentException("Not found identifier") } } case Plus(e1, e2) => { val v1 = evalExpr(e1, env) val v2 = evalExpr(e2, env) (v1, v2) match { case (NumValue(f1), NumValue(f2)) => NumValue(f1 + f2) case _ => throw new IllegalArgumentException("plus failed") } } case Mult(e1, e2) => { val v1 = evalExpr(e1, env) val v2 = evalExpr(e2, env) (v1, v2) match { case (NumValue(f1), NumValue(f2)) => NumValue(f1 * f2) case _ => throw new IllegalArgumentException("mult failed") } } case Let(x, e1, e2) => { // YOUR CODE HERE val v1 = evalExpr(e1, env) val newEnv = env + (x -> v1) evalExpr(e2, newEnv) } case MultiLet(xList, eList, e2) => { // YOUR CODE HERE val vList = eList.map(evalExpr(_, env)) val newEnv = xList.zip(vList).foldLeft(env) { case (accEnv, (x, v)) => accEnv + (x -> v) } evalExpr(e2, newEnv) } } }

image text in transcribedimage text in transcribed

[ ]:

sealed trait Expr
type Tag = String 
type Identifier = String 
 
case class Const(d: Double) extends Expr
case class Ident(id:Identifier, tag: Tag) extends Expr
case class Plus(e1: Expr, e2: Expr) extends Expr
case class Mult(e1: Expr, e2: Expr) extends Expr 
case class Let(id: Identifier, tag: Tag, e1: Expr, e2: Expr) extends Expr
 
// Some helpful functions to write test cases -- you can ignore these.
 
def x(tag:String):Expr = Ident("x", tag)
def y(tag:String):Expr = Ident("y", tag)
def z(tag:String):Expr = Ident("z", tag)
def w(tag:String):Expr = Ident("w", tag)
 
 
 

[Feel in the ??? ]:

def getAllUseDefLinks(e: Expr, env: Map[Identifier, Tag]): Map[Tag, Tag] = 
 
 e match {
 case Const(_) => { Map.empty }
 case Ident(id, usetag) => {
 // YOUR CODE HERE
 ???
 }
 
 case Plus(e1, e2) => {
 // YOUR CODE HERE
 ???
 }
 
 case Mult(e1, e2) => {
 // YOUR CODE HERE
 ???
 }
 
 case Let(x, tag, e1, e2) => {
 // YOUR CODE HERE
 ???
 }
 
 
}
 

[ test cases ]:

/*
 let y@tag2 = x @use0 in 
 let x @tag1 = 10 in 
 x@use1 + x@use2
*/
val e = Let("y", "tag2", x("use0"), Let ("x", "tag1", Const(10), Plus(x("use1"), x("use2"))))
val map = getAllUseDefLinks(e, Map.empty)
 
assert (map("use0") == "UNDEFINED", s"Test1 failed: use0 must be associated with UNDEFINED. Your code returns ${map("use0")}")
assert (map("use1") == "tag1", s"Test1 failed: use0 must be associated with UNDEFINED. Your code returns ${map("use1")}")
assert (map("use2") == "tag1", s"Test1 failed: use0 must be associated with UNDEFINED. Your code returns ${map("use2")}")
 
passed(5)

[ test cases ]:

/* 
let x @tag3 = x @use0 + 10 in 
 let y@tag1 = 10 in 
 x@use1 * y @use2 */
 
val e = Let("x", "tag3", Plus(x("use0"), Const(10.0)), 
 Let ("y", "tag1", Const(10), Mult(x("use1"), y("use2"))))
val map = getAllUseDefLinks(e, Map.empty)
assert (map("use0") == "UNDEFINED", s"Test1 failed: use0 must be associated with UNDEFINED. Your code returns ${map("use0")}")
assert (map("use1") == "tag3", s"Test1 failed: use1 must be associated with UNDEFINED. Your code returns ${map("use1")}")
assert (map("use2") == "tag1", s"Test1 failed: use2 must be associated with tag1. Your code returns ${map("use2")}")
 
passed(5)
Semantics for MultiLet Let us write down the semantic rules for a multilet statement The semantic rule above telk you to - Evaluate each of the expressions from e1,..., en under the environment env. - Next, if all the expressions above evaluated without an error, it tells you to update the map env by binding each xi to Dk, the result of evaluating ei. . You can use the Scala Map "++" operator to achieve this in one shot. - Finally, you should evaluate eBody under the new ervironment created For convenience, we write a single "generic" semantic rule that shows that if some argument ef evaluates to an error, the whole expression is erroneous. Interpreter for Multilet Statements Implement an interpreter for the lettuce language with multi-let. statements. Your interpreter does not need to "propagate" error. instead you should throw an IllegalArgumentException whenever an error is encountered. Style Guide Use of var/whileffor loops in your solution below is disallowed. Problem 2: Tag Usages of Identifiers with Definitions In this problem, we will extend Lettuce by adding user-define generated (https//en.wikipedia.org/wiki/Use-define_chain). Example 1 In this example, we add "comment tagg" to each let binding (tagi, tag2, and tag3) and to each usage ( use1, use2, use3). The togs play no role in the program execution other than giving names to refer to the appropriate definitions and usages of identifiers. The purpose of this analysis is to produce a Map that maps usage tags to the let binding tags. This denotes that the usage tagged "use1" corresponds to the let-binding "tag3", "use2" usage corresponds to let binding "tog1" and "use3" usage corresponds to let binding "tag2". Abstract Syntax - First compute the use-def mappings for the RHS expression e1. - Nirst compute update the enwironment to note that identifier 18 is associated with tag. - Now, compute the mappings for in" exp - Conjoin the two maps for e1 and e2. Here are two nules for a identifiers. getAllseDefLinks(Ident(1d,usetag),env)={usetag"UNDENINED"}1d/domsin(env)(ident-undefined) Note that we reserve a special tag name UADEFINED (i.e, the string UNDEFINED") to denote identifiers that are not defined in the current scope - If the identifier Id does not belong to the domain of the environment, then retum a singleton mapping from the use-tag usetag associated with this use of id to the special tag unoEFINED: Part (A) : Complete the rule for Plus ( 5 points). Write down what should go in for ?1 and ?2 below. - If there are no antecedents just write "blank". - The empty map is represented as \{\} . - Concatenation of two maps is nap1 and map 2 is denoted map1 + map2: the scala operator for this is ++ though. YOUR ANSWER HERE Complete the rule for Const Write down what should go in for ?1 and 2 below. - If there are no antecedents just write "blank". - The empty map is represented as \{\} . - Concatenation of two maps is map1 and map2 is denoted nap1 + map2 . YOUR ANSWER HERE

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

Students also viewed these Databases questions