
From int-e@gmx.de Sun Apr 24 21:13:18 2016 Date: Sun, 24 Apr 2016 21:13:18 +0200 From: Bertram Felgenhauer
To: haskell-cafe@haskell.org Subject: Re: [Haskell-cafe] Is it possible to make lazy combinators for IO? Message-ID: <20160424191319.GA32509@24f89f8c-e6a1-4e75-85ee-bb8a3743bb9f> References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.24 (2015-08-30) Status: RO Content-Length: 1283 Lines: 37
David Feuer wrote:
What I'm looking for is more limited than lazy IO or unsafeInterleaveIO, but it seems quite possible that there's no way to get just what I'm looking for with the IO type proper using GHC's implementation of IO. Lazy IO allows evaluation to drive action. When a thunk is forced, it may trigger I/O (spooky action at a distance). What I'm talking about is separating what actions are performed from what values are calculated from them. Here's a partial concept which won't actually compile because of the lazy pattern matches:
data MyIO a = forall b . MyIO (b -> a) (IO b) instance Functor MyIO where fmap f ~(MyIO t m) = MyIO (f . t) m instance Applicative MyIO where pure a = MyIO (const a) (pure ()) MyIO t1 m1 <*> ~(MyIO t2 m2) = MyIO (\(r1, r2) -> t1 r1 (t2 r2)) ((,) <$> m1 <*> m2) instance Monad MyIO where ??? instance MonadFix MyIO where ???
I believe that using this interface `unsafeInterleaveIO` could be implemented as follows, making it just as powerful as lazy IO: data Box a = Box a unsafeInterleaveMyIO :: MyIO a -> MyIO a unsafeInterleaveMyIO act = do act' <- Box `fmap` act return $ case act' of Box !r -> r Have I missed anything? Cheers, Bertram
participants (1)
-
None