I'm new to Haskell and can't figure out if I'm mixing up concepts or if it's just a syntax error eluding me.
I have a function which expects to return an "Expr" type - a custom data type I've defined in a separate module. Expr has multiple constructors, one of which is LiteralValue. In this function, if I run into an EOF token, I want to return a LiteralValue Expr with value NONE.
expression :: [Token] -> Expr
expression (head:remainder)
| checkType head EOF = LiteralValue {value = NONE}
| otherwise = equality (head:remainder)
Meanwhile, here is the definition for Expr. It uses record syntax and has multiple constructors. (Token and Literal are both defined elsewhere)
data Expr =
Binary {left :: Expr, operator :: Token, right :: Expr} |
Grouping {expression :: Expr} |
LiteralValue {value :: Literal} |
Unary {operator :: Token, right :: Expr}
I keep trying to figure out how to instantiate an Expr type, but it doesn't seem to want to accept it. Right now, it's telling me both LiteralValue and value aren't in scope. How do I go about creating one of these types?
Edit: Here's what is imported and exported: The expression function is in a file Parser.hs:
import ParseTree
import Tokens
ParseTree contains the Expr data type and Tokens contains Token, Literal, and values like NONE. In ParseTree.hs, we find:
module ParseTree (A, Expr) where
import Tokens
I'm able to access Expr in Parser and anything from the Tokens.hs file, just not these subtypes of Expr.
The problem probably is not with the Haskell grammar, but the fact that LiteralEval
and NONE
are not in scope. You need to import these, like:
import SecondModule (Expr (LiteralValue))
import ThirdModule (Literal (NONE))
-- …