Why not assign a type to unsafePerformIO?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Why not assign a type to unsafePerformIO?

Justin Bailey
One of the "holes" in real-world Haskell is you never know if a library/function is calling unsafePerformIO and you have to trust the library author. I recognize the necessity of the function, but should it announce itself? unsafePerformIO has this type:

  unsafePerformIO :: IO a -> a

Would there be any value to making it have a type that can be stripped off, like some other monads? For example, providing a "runUnsafe" or similar:

  data UnsafePerformIO a = Unsafe a

  runUnsafe :: UnsafePerformIO a -> a
  runUnsafe (Unsafe o) = o

and changing unsafePerformIO to have the type:

  unsafePerformIO :: IO a -> UnsafePerformIO a

It seems it would be valuable to have functions announce when they use unsafePerformIO, but additionally allow it to be stripped off. So the classic

  launchMissiles :: a -- Uses unsafePerfomIO!

Would become

  launchMissiles :: UnsafePerformIO a

Which could be stripped off it you wanted:

  evilDictatator :: a
  evilDictator = runUnsafe $ launchMissiles

But doesn't have to be:

  incompetentDictator :: a
  incompetentDictator = launchMissiles -- Doesn't type check!

I doubt this is original - does it buy anything?

Justin


_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Why not assign a type to unsafePerformIO?

Justin Bailey
On 10/3/07, Victor Nazarov <[hidden email]> wrote:
But how would you know that evil dictator uses unsafePerformIO???

You don't. unsafePerformIO  can't be taken it away (there are legitimate reasons to strip IO), which is why I wonder if it's useful at all.

p.s. CC'ed to haskell-cafe

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Why not assign a type to unsafePerformIO?

Bugzilla from jonathanccast@fastmail.fm
In reply to this post by Justin Bailey
On Wed, 2007-10-03 at 14:47 -0700, Justin Bailey wrote:

> One of the "holes" in real-world Haskell is you never know if a
> library/function is calling unsafePerformIO and you have to trust the
> library author. I recognize the necessity of the function, but should
> it announce itself? unsafePerformIO has this type:
>
>   unsafePerformIO :: IO a -> a
>
> Would there be any value to making it have a type that can be stripped
> off, like some other monads? For example, providing a "runUnsafe" or
> similar:
>
>   data UnsafePerformIO a = Unsafe a
>
>   runUnsafe :: UnsafePerformIO a -> a
>   runUnsafe (Unsafe o) = o
>
> and changing unsafePerformIO to have the type:
>
>   unsafePerformIO :: IO a -> UnsafePerformIO a
>
> It seems it would be valuable to have functions announce when they use
> unsafePerformIO, but additionally allow it to be stripped off. So the
> classic
>
>   launchMissiles :: a -- Uses unsafePerfomIO!
>
> Would become
>
>   launchMissiles :: UnsafePerformIO a
>
> Which could be stripped off it you wanted:
>
>   evilDictatator :: a
>   evilDictator = runUnsafe $ launchMissiles
>
> But doesn't have to be:
>
>   incompetentDictator :: a
>   incompetentDictator = launchMissiles -- Doesn't type check!
>
> I doubt this is original - does it buy anything?
>
This already exists.  The monad is called IO, the unsafePerformIO
implementation is called id, and runUnsafe is called unsafePerformIO.  I
really don't see how another set of aliases buys anything.

jcc


_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Why not assign a type to unsafePerformIO?

Victor Nazarov-2
In reply to this post by Justin Bailey
On 10/4/07, Justin Bailey <[hidden email]> wrote:
> On 10/3/07, Victor Nazarov <[hidden email]> wrote:
> > But how would you know that evil dictator uses unsafePerformIO???
>
> You don't. unsafePerformIO  can't be taken it away (there are legitimate
> reasons to strip IO), which is why I wonder if it's useful at all.
>
> p.s. CC'ed to haskell-cafe
>

May be you should be interested in Tom Mortel's example of using
unsafeInterleaveIO:
http://blog.moertel.com/articles/2007/03/28/directory-tree-printing-in-haskell-part-three-lazy-i-o

If you don't mean anything like this, I don't understand your intent.

--
vir
http://vir.comtv.ru/
_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Why not assign a type to unsafePerformIO?

Bulat Ziganshin-2
In reply to this post by Justin Bailey
Hello Justin,

Thursday, October 4, 2007, 1:47:00 AM, you wrote:

> Which could be stripped off it you wanted:

>   evilDictatator :: a
>   evilDictator = runUnsafe $ launchMissiles

just imagine using ByteString library with all these runUnsafe calls
flying around :)))  or using immutable arrays - in Hugs, they are
implemented using unsafeIOToST. and the last point - because
almost everything is based on using C calls and they *can* launch
missiles, you should have *every* library function imported to be in
these Unsafe monad. are you really need it? :D


--
Best regards,
 Bulat                            mailto:[hidden email]

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe