
On Mon, Oct 6, 2008 at 18:48, Ryan Ingram
Also, you can get the same behavior out of "ta" if you write it like this:
ta ~(A x) = True
The translation looks something like this:
f ~(A x) = e => f a = e where x = case a of (A v) -> v -- a,v fresh variables not mentioned in e
(or, equivalently)
f a = let (A x) = a in e -- "a" some fresh variable not mentioned in e
This delays the pattern-matching (and thus, the evaluation of "a") lazily until "x" is demanded, at which point "a" might throw an exception or infinite loop, or, if the type has more than one constructor, fail to pattern match (which also throws an exception). If "x" is never demanded then neither is "a".
Ah, that's pretty neat and subtle. Now, say I have a type created with data that has only one constructor. Couldn't the compiler optimize away this unneccessary evaluation during pattern matching? I.e. it would make what you just wrote implicit in that case. Or perhaps data declarations with just one ctor should really be turned into newtypes by the programmer? cheers, Arnar