0
votes

In the process of writing an interpreter in Haskell for a separate, simple programming language - I find myself butting my head against a wall as I learn typing in Haskell.

I have two custom data types

data Expr
    = Var Var
    | NumE Int
    | NilE
    | ConsE Expr Expr
    | Plus Expr Expr
    | Minus Expr Expr
    | Times Expr Expr
    | Div Expr Expr
    | Equal Expr Expr
    | Less Expr Expr
    | Greater Expr Expr
    | Not Expr
    | Isnum Expr
    | And Expr Expr
    | Or Expr Expr
    | Head Expr
    | Tail Expr
    | Call String
    deriving (Show, Read)

data Val = Num Int | Nil | Cons Val Val
    deriving (Eq, Show, Read)

and I'm starting to write the cases for interpreting these options, with the function interpret_expr

interpret_expr :: Vars -> Expr -> Val
interpret_expr vars@(Vars a b c d) (NumE integer) = integer

but this COMPLAINS that it couldn't match expected type 'Val' with actual type 'Int' in the expression 'integer'. But say I change it to something silly like

interpret_expr :: Vars -> Expr -> Val
interpret_expr vars@(Vars a b c d) (NumE 'a') = 'a'

it then complains at 'a' that it can't match expected type 'Int' with actual type 'Char'. NOW IT WANTS AN INT?????? I really don't know what to say, I really thought it would be as simple as providing NumE with a variable it could figure is an integer. What am I doing wrong?

1
What is the Val type? You don't show that definition.Scott Olson
In your first interpret_expr, you said it should return a Val, but you are trying to return an Int. You forgot to apply your constructor Num : Int -> Val.luqui

1 Answers

5
votes

In the first case you are returning an Int from a function you declared to return a Val. From your definition of Val it looks like you probably want to return Num integer here.

In the second case the problem is in the pattern matching. (NumE 'a') is an error because NumE is defined as NumE Int, so it must be followed by an Int, not a Char.