Neil Mitchell | 4 Mar 2008 17:17
Picon
Gravatar

Re: coreFunc has extra argument

Hi

>   showsType io = showString "(IO " . showsType a . showChar ')'
>        where
>        a = unsafePerformIO io

You don't even need unsafePerformIO! All you need is a proxy a which
has the right type, this can be done by doing:

showsType io = showString "(IO " . showsType a . showChar ')'
     where
     a = undefined
     b = asTypeOf (return a) io

i.e. instead of unwrapping the io, you wrap up the a, and demand it
has the same type as io. Same result, but now entirely "safe".

[Note: entirely untested, but the idea should be sound]

Thanks

Neil
Tom Shackell | 4 Mar 2008 17:22
Picon

Re: coreFunc has extra argument

Neil Mitchell wrote:
> You don't even need unsafePerformIO! All you need is a proxy a which
> has the right type, this can be done by doing:
> 
> showsType io = showString "(IO " . showsType a . showChar ')'
>      where
>      a = undefined
>      b = asTypeOf (return a) io
> 

That works too, though it still relies on showsType not evaluating it's 
argument - since otherwise it'll eval undefined :S

Tom
Neil Mitchell | 4 Mar 2008 17:26
Picon
Gravatar

Re: coreFunc has extra argument

Hi

>  > showsType io = showString "(IO " . showsType a . showChar ')'
>  >      where
>  >      a = undefined
>  >      b = asTypeOf (return a) io
>  >
>
>  That works too, though it still relies on showsType not evaluating it's
>  argument - since otherwise it'll eval undefined :S

Of course, but the advantage is that if it fails to work the side
effect is _|_, rather than (potentially) rooting your computer. I'm
thinking for the day when we have yhi --safe to not run IO
computations.

i.e. showType (deleteFile "/usr/shackell/thesis.tex")

:-)

Thanks

Neil

Gmane