Re: More static type fun.

From: Darius (ddarius_at_hotpop.com)
Date: 11/16/03


Date: Sat, 15 Nov 2003 19:41:46 -0500

On Sat, 15 Nov 2003 22:46:33 +0100
Dirk Thierbach <dthierbach@gmx.de> wrote:

> In pure Haskell or OCaml, using them doesn't really make sense --
> either you convert between the same types, and then you don't have
> to do the conversion, or you convert between different types, which
> means your program will crash horribly as soon as the bitpattern
> is now interpreted in a wrong way.
>
> - Dirk

Not necessarily.

Why go to all that trouble pattern matching a list and building
something of type Maybe when you can just coerce them! (I finally
actually found GHC's unsafeCoerce#)

Prelude> :m Maybe
Prelude Maybe> listToMaybe [] :: Maybe ()
Nothing
Prelude Maybe> listToMaybe [1]
Just 1
Prelude Maybe> listToMaybe [1,2]
Just 1
Prelude Maybe> :m GHC.Base
Prelude GHC.Base> :set -fglasgow-exts
Prelude GHC.Base> let fastListToMaybe :: [a] -> Maybe a;fastListToMaybe
= unsafeCoerce#
Prelude GHC.Base> fastListToMaybe [] :: Maybe ()
Nothing
Prelude GHC.Base> fastListToMaybe [1]
Just 1
Prelude GHC.Base> fastListToMaybe [1,2]
Just 1

The x86 ASM and C programmer inside of me revels.