Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to split up a file into modules in Haskell?

Tags:

haskell

I'm having an issue with the syntax for modules. Basically I'm trying to split my code into two seperate files, one for the object I'm creating (AST), and one for all of my functions.


    --main.hs
    data AST = Add (AST) (AST)|
               Sub  (AST) (AST)|
               Mult (AST) (AST)|
               Ident Char|
               Num Int
               deriving Show

    aSTLeft (Num    l   ) = (Num l)
    aSTLeft (Ident  l   ) = (Ident l)
    aSTLeft (Add  l _   ) = l
    aSTLeft (Sub  l _   ) = l
    aSTLeft (Mult l _   ) = l
    aSTRight    (Num    r   ) = (Num r)
    aSTRight    (Ident  r   ) = (Ident r)
    aSTRight    (Add  _ r   ) = r
    aSTRight    (Sub  _ r   ) = r
    aSTRight    (Mult _ r   ) = r

    isNum (Num x) = True
    isNum (Ident x) = False
    isNum (Add  (x)(y)) = False
    isNum (Sub  (x)(y)) = False
    isNum (Mult (x)(y)) = False

    --a lot more functions here

This works fine, but when I try to split the AST datatype into a seperate file,


    --ASTADT.hs
    module ASTADT (AST,aSTLeft,aSTRight) where
    data AST =    Add (AST) (AST)|
                  Sub  (AST) (AST)|
                  Mult (AST) (AST)|
                  Ident Char|
                  Num Int
                  deriving Show

    aSTLeft     (Num    l   ) = (Num l)
    aSTLeft     (Ident  l   ) = (Ident l)
    aSTLeft     (Add  l _   ) = l
    aSTLeft     (Sub  l _   ) = l
    aSTLeft     (Mult l _   ) = l
    aSTRight        (Num    r   ) = (Num r)
    aSTRight        (Ident  r   ) = (Ident r)
    aSTRight        (Add  _ r   ) = r
    aSTRight        (Sub  _ r   ) = r
    aSTRight        (Mult _ r   ) = r

and


    --main.hs
    import ASTADT
    isNum (Num x) = True
    isNum (Ident x) = False
    isNum (Add  (x)(y)) = False
    isNum (Sub  (x)(y)) = False
    isNum (Mult (x)(y)) = False
    --a lot more functions here

Loading main.hs I get error message


    Undefined data constructor "Num"

I'm pretty sure its just an issue with my module syntax, but I've been completely stuck for several hours now. I'm working with Hugs. Thanks

like image 839
Raymond Law Avatar asked Mar 17 '26 12:03

Raymond Law


2 Answers

Module ASTADT exports just the AST data type, but not its constructors. This is useful if you want to hide the implementation details of your data types from the modules users.

In your case you don't want that, so to export the constructors you can specify them in the module ... where line after the data type, like AST (Num, Ident). For exporting all the constructors of the data type you can use (..) instead of naming them all explicitly:

module ASTADT (AST (..), aSTLeft, aSTRight) where
...
like image 140
sth Avatar answered Mar 20 '26 05:03

sth


Your export declaration for the data constructors should look something like

module ASTADT (AST (Add, Sub, Mult, Ident, Num), aSTLeft, aSTRight) where
like image 33
Corey Porter Avatar answered Mar 20 '26 05:03

Corey Porter



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!