[Haskell-cafe] Data & newtype differences. Today: strictness

Erik Hesselink hesselink at gmail.com
Tue Jan 24 13:45:47 CET 2012


An interesting use case for this is that while

  data Void = Void Void

has infinitely many values (undefined, Void undefined, Void (Void
undefined) etc), the newtype version

  newtype Void = Void Void

has only one, bottom. This is a way to define the empty datatype
without extensions.

Erik

On Tue, Jan 24, 2012 at 11:25, Ketil Malde <ketil at malde.org> wrote:
> Yves Parès <yves.pares at gmail.com> writes:
>
>> I had for long thought that data and newtype were equivalent, but then I
>> spotted some differences when it comes to strictness.
>>
>> data Test = Test Int
>> newtype TestN = TestN Int
>
> Interesting.  I'd thought that
>
>  data Test = Test !Int
>
> and
>
>  newtype Test = Test Int
>
> would be equivalent, but here you (well, I had to add the ! myself) show
> a situation where they're not.
>
> I guess
>
>  pm (Test _) = 12
>
> is the same as
>
>  pm = \x -> case x of Test _ -> 12
>
> which perhaps makes it clearer why it breaks on undefined...
>
> -k
> --
> If I haven't seen further, it is by standing in the footprints of giants
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe



More information about the Haskell-Cafe mailing list