Implementing algebraic data types to my compiler

Viewed 572

I've been trying to write a small compiler for the past few weeks, while reading Stephen Diehl's great tutorial "Write You A Haskell". I'm currently writing an interpreter before writing the compiler.

I have troubles to represent my values evaluated from an algebraic data type constructor application (e.g. in Haskell Just 1, Left "error", etc...).

My values are currently represented by the following type:

data Value
    = Int Integer
    | Flt Double
    | Chr Char
    | Str String
    | Lam Name AST.Expr (Scope Value)
    | HLam (Value -> Value)

How would one implement type constructors ?

So far I've thought of this:

data Value
    = Int Integer
    | Flt Double
    | Chr Char
    | Str String
    | App Constr [Value]  -- Constr applied of [Value]
    | Lam Name AST.Expr (Scope Value)
    | HLam (Value -> Value)

-- Constructor ID and signature
-- would be translated to a lambda abstraction
data Constr = Constr Name [Type]

But I would have to check every time I work with the constructor if the lengths of the signature and the number of applied values are equal.

Is there another solution ?

Thanks in advance.

1 Answers
Related