[Haskell-cafe] a really juvenile question .. hehehehe ;^)

Arnar Birgisson arnarbi at gmail.com
Mon Oct 6 10:30:00 EDT 2008


Hi,

On Mon, Oct 6, 2008 at 16:10, Ryan Ingram <ryani.spam at gmail.com> wrote:
> On Mon, Oct 6, 2008 at 2:19 PM, Arnar Birgisson <arnarbi at gmail.com> wrote:
>> And this requirement is there why? Is it specifically put in so that
>> one is able to create this overhead-less implementation?
>>
>> Given:
>>
>> data A = A Int
>> newtype B = B Int
>>
>> ta (A x) = True
>> tb (B x) = True
>>
>> This happens (not surprisingly given your above comments):
>>
>> *Main GOA> :load test.hs
>> [1 of 1] Compiling Main             ( test.hs, interpreted )
>> Ok, modules loaded: Main.
>> *Main GOA> ta undefined
>> *** Exception: Prelude.undefined
>> *Main GOA> tb undefined
>> True
>>
>> Why is the x evaluated in ta?
>
> x isn't evaluated.

Yes, realized my error just after hitting send :/

> "undefined" is evaluated to see if it matches the
> constructor "A".  But we don't even get to check, because undefined
> throws an exception during its evaluation.
>
> In the "tb" case, (B x) always matches because B is a newtype.  x gets
> bound to undefined, but never evaluated.

And this happens because data values are basically pattern matched at
run-time but newtype values are matched at compile-time, effectively
turning tb into an Int -> Bool function?

That explains pretty well why newtype can have only one constructor.

cheers,
Arnar


More information about the Haskell-Cafe mailing list