How should I implement scala interpreter for tuple?

How should I implement scala interpreter for tuple?

Try to replace

case class TupleV(values: List[Type]) extends Value

case class TupleE(expressions: List[Type]) extends Expr

with

case class TupleV(values: List[Value]) extends Value 

case class TupleE(expressions: List[Expr]) extends Expr

(Are those definitions given to you in an exercise or are they your own definitions that you can modify?)

Based on

case TupleE(values)=>TupleV(values)

youre writing interpreter

def interpret(expr: Expr): Value

i.e. continue what you started in How should I implement add interpreter in scala?

Then the definitions of TupleV, TupleE should be latter above. Former their definitions above make less sense to me.

Type is needed when you typecheck an Expr

def typecheck(expr: Expr): Type // or Option[Type]

Then youll have one more hierarchy

case class TupleT(types: List[Type]) extends Type

So, I implemented as followed:

case TupleE(values)=>TupleV(values)

but it says List[Expr] found, List[Value] is required. What is the
problem in my code? what should I to to fix it?

This is irreproducible. The code compiles

https://scastie.scala-lang.org/KWlyFOYDRHOgN6UibVY1pw

Using that, I tried

 case TupleE(expression)=>expression match{   
   case ConsE(head, tail)=>ConsV(head, tail)
 }

But it returns: constructor cannot be instantiated to expected type
error for ConsE.

This error is clear: in TupleE(expression) expression has type List[Type], it cant match ConsE, it can match only ordinary scala List.

How should I implement scala interpreter for tuple?

Leave a Reply

Your email address will not be published.