While I was writing a Haskell program, I've thought about language construct
which would be of use to me (and perhaps to the others). We have the error
function:
error :: String -> a
It is useful as a "hole" - when I have a datatype tree, like:
data A = A B Int
data B = B C String
data C = ...
and so on, and I don't know all the informations for the datatype value at some
point in the program (at the end of the execution, however, they will be known),
I can make a function:
hole = error "Internal error. This should never be evaluated"
and construct values like:
A (B hole "foo") hole
The real life example is parsing in a compiler - parser builds a program tree yet
the information of types of expressions, which has to be in this tree to
generate code, is not known during parsing.
Of course, I could use two separate trees - but it's awkward,
duplicating names etc.
Now, when I use hole function, I cannot run only
parsing phase - when I write: 'parse "input.c"' it stops - no surprise -
when it tries to show the hole. (parse returns a program tree)
The construct I want is:
error1 :: String -> String -> a
where the first string says what should be seen (without stopping the
program) when one tries to show it, the other what should be seen (with
stopping the program) when trying to evaluate it. Or, even better, (but it's
probably not possible), an Error type constructor:
Error :: String -> a
, so that it would be possible to use it just like the error function, but also
to instantiate classes with it, i.e.:
instance Show Error where
show (Error s) = show s
instance Eq Error where
Error s == Error s1 = s == s1
Would it be possible to add sth like that easily to the existing
compiler/interpreter (for example ghc)?
Wojciech Moczydlowski, Jr