1

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.

2
  • The code for expression looks fine in that respect. The error you describe suggests the module you imported doesn't explicitly export the data constructors, which means you are probably supposed to use some other function to create a value of type Expr. Commented Feb 26, 2024 at 21:36
  • Thank you, swapped the images to code snippets. Also added more information on what is imported and exported. Commented Feb 26, 2024 at 23:31

1 Answer 1

1

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))

-- …
Sign up to request clarification or add additional context in comments.

1 Comment

Aha! It seems the module with the Expr data type had an export that was too restrictive: "module ParseTree (A, Expr) where" By removing the parenthesized items or including more of them, they can now be properly detected in the main module. It seems it was indeed a scope error. Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.