As the title says im trying to build an interpreter for Imperative languages in Haskell. Iv done 90% of it, nevertheless, im trying to build If statements, and my question is, how will i define a new datatype lets say :
data x = if boolExp then exp else exp.
I understand that i could re-write this with something like doIf boolExp exp exp. But i would like to see if i could use those reserved keywords just for fun (and maybe conciseness).
Note that both boolExp and exp are defined in my language and work correctly( i even evaluate them to get the actual expression “value”).
So bottom line is, how will i add reserved keywords in my data definition as required above?
The closest you can get is the
RebindableSyntaxextension in GHC, which treatsif cond then truePart else falsePartas if you had writtenifThenElse cond truePart falsePart, with whateverifThenElseis in scope.You can’t use this for a data constructor, but you can write something like this:
Running this prints
If Foo Bar Bar.However, unless you’re writing some kind of internal DSL where this would make sense, I strongly recommend just sticking with the regular syntax like
If cond truePart falsePart. Writing it this way has no real benefit and serves only to confuse people when reading your code.