Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extracting Information from Haskell Object

Tags:

haskell

I'm new to Haskell and I'm confused on how to get values out of function results. In my particular case, I am trying to parse Haskell files and see which AST nodes appear on which lines. This is the code I have so far:

import Language.Haskell.Parser
import Language.Haskell.Syntax

getTree :: String -> IO (ParseResult HsModule)
getTree path = do
               file <- readFile path
               let tree = parseModuleWithMode (ParseMode path) file
               return tree

main :: IO ()
main = do
       tree <- getTree "ex.hs"
       -- <do something with the tree other than print it>
       print tree

So on the line where I have the comment, I have a syntax tree as tree. It appears to have type ParseResult HsModule. What I want is just HsModule. I guess what I'm looking for is a function as follows:

extract :: ParseResult a -> a

Or better yet, a general Haskell function

extract :: AnyType a -> a

Maybe I'm missing a major concept about Haskell here?

p.s. I understand that thinking of these things as "Objects" and trying to access "Fields" from them is wrong, but I'd like an explanation of how to deal with this type of thing in general.

like image 214
nullromo Avatar asked Dec 17 '25 21:12

nullromo


1 Answers

Looking for a general function of type

extract :: AnyType a -> a

does indeed show a big misunderstanding about Haskell. Consider the many things AnyType might be, and how you might extract exactly one object from it. What about Maybe Int? You can easily enough convert Just 5 to 5, but what number should you return for Nothing?

Or what if AnyType is [], so that you have [String]? What should be the result of

extract ["help", "i'm", "trapped"]

or of

extract []

?

ParseResult has a similar "problem", in that it uses ParseOk to contain results indicating that everything was fine, and ParseFailed to indicate an error. Your incomplete pattern match successfully gets the result if the parse succeeded, but will crash your program if in fact the parse failed. By using ParseResult, Haskell is encouraging you to consider what you should do if the code you are analyzing did not parse correctly, rather than to just blithely assume it will come out fine.

like image 176
amalloy Avatar answered Dec 20 '25 18:12

amalloy



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!