Skip to content

Commit f377555

Browse files
committed
Compatibility of IOMonad with std-lib v2.0 (if they accept my PR)
See agda/agda-stdlib#2125
1 parent 08ff2fc commit f377555

File tree

1 file changed

+40
-1
lines changed

1 file changed

+40
-1
lines changed

src/agda/Library.agda

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -406,8 +406,10 @@ module ErrorMonad {e} {E : Set e} where
406406
catchError (fail e) h = h e
407407
catchError (ok a) h = ok a
408408

409+
open import Category.Functor using (RawFunctor)
410+
open import Category.Applicative using (RawApplicative)
409411

410-
module IOMonad where
412+
module IOFunctor where
411413
open import IO.Primitive public using (return; _>>=_)
412414

413415
infixl 1 _>>_
@@ -420,6 +422,43 @@ module IOMonad where
420422
_=<<_ : {a b} {A : Set a} {B : Set b} (A IO B) IO A IO B
421423
k =<< m = m >>= k
422424

425+
-- The following definition is needed to create the RawFunctor instance.
426+
427+
infixl 4 _<$>_
428+
429+
_<$>_ : {a b} {A : Set a} {B : Set b} (A B) IO A IO B
430+
f <$> m = m >>= λ a -> return (f a)
431+
432+
-- This module is needed to create the RawApplicative instance
433+
-- in a way compatible with both std-lib v1.7.1/2 and v2.0.
434+
435+
module IOApplicative where
436+
open IOFunctor public
437+
438+
rawFunctor : {ℓ} RawFunctor (IO {a = ℓ})
439+
rawFunctor = record { IOFunctor }
440+
441+
pure : {a} {A : Set a} A IO A
442+
pure = return
443+
444+
infixl 4 _<*>_ _⊛_
445+
446+
_<*>_ : {a b} {A : Set a} {B : Set b} IO (A B) IO A IO B
447+
mf <*> ma = mf >>= λ f ma >>= λ a return (f a)
448+
449+
_⊛_ : {a b} {A : Set a} {B : Set b} IO (A B) IO A IO B
450+
_⊛_ = _<*>_
451+
452+
module IOMonad where
453+
open IOApplicative public
454+
455+
-- Field rawApplicative is part of the RawMonad of std-lib v2.0
456+
-- Thus we have to construct the Applicative implementation
457+
-- even though we do not use it.
458+
--
459+
rawApplicative : {ℓ} RawApplicative (IO {a = ℓ})
460+
rawApplicative = record { IOApplicative }
461+
423462
{-# FOREIGN GHC import qualified Data.List #-}
424463
{-# FOREIGN GHC import qualified Data.Text #-}
425464
{-# FOREIGN GHC import qualified Data.Text.IO #-}

0 commit comments

Comments
 (0)