Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions lib/Haskell/Control/Applicative.agda
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
-- For some and many.
{-# OPTIONS --no-termination-check #-}

module Haskell.Control.Applicative where

open import Haskell.Prim
open import Haskell.Prim.Functor
open import Haskell.Prim.Applicative
open import Haskell.Prim.Maybe
open import Haskell.Prim.Either

-- ** base
record Alternative (f : Set → Set) : Set₁ where
infixl 3 _<|>_
field
empty : f a
_<|>_ : f a → f a → f a
overlap ⦃ super ⦄ : Applicative f
some : f a → f (List a)
many : f a → f (List a)
-- ** defaults
record DefaultAlternative (f : Set → Set) : Set₁ where
infixl 3 _<|>_
field
empty : f a
_<|>_ : f a → f a → f a
overlap ⦃ super ⦄ : Applicative f

-- | One or more.
some many : f a -> f (List a)
some v = _<*>_ (fmap _∷_ v) (many v)
many v = some v <|> pure []

-- ** export
open Alternative ⦃...⦄ public
{-# COMPILE AGDA2HS Alternative existing-class #-}

-- ** functions
optional : {f : Set → Set} {{altf : Alternative f}} → f a → f (Maybe a)
optional v = Just <$> v <|> pure Nothing

-- ** instances
private
mkAlternative : DefaultAlternative t → Alternative t
mkAlternative x = record {DefaultAlternative x}
instance
open DefaultAlternative

iAlternativeMaybe : Alternative Maybe
iAlternativeMaybe = mkAlternative λ where
.empty → Nothing
._<|>_ Nothing m2 → m2
._<|>_ (Just a1) _ → Just a1