Skip to content

Instantly share code, notes, and snippets.

@AndrasKovacs
Last active August 29, 2015 14:05
Show Gist options
  • Save AndrasKovacs/53c1a9f61a7fe3d6f6a5 to your computer and use it in GitHub Desktop.
Save AndrasKovacs/53c1a9f61a7fe3d6f6a5 to your computer and use it in GitHub Desktop.
List is a monad.
open import Data.List
open import Function
open import Relation.Binary.PropositionalEquality
open import Data.Product
open import Algebra
module LM {a}{A : Set a} = Monoid (monoid A)
infixr 5 _>>=_
_>>=_ : ∀ {a b}{A : Set a}{B : Set b} → List A → (A → List B) → List B
_>>=_ [] f = []
_>>=_ (x ∷ xs) f = f x ++ (xs >>= f)
return : ∀ {a}{A : Set a} → A → List A
return x = x ∷ []
left-ret : ∀ {a b}{A : Set a}{B : Set b}(f : A → List B)(x : A) → return x >>= f ≡ f x
left-ret f x = proj₂ LM.identity (f x)
right-ret : ∀ {a}{A : Set a}(xs : List A) → xs ≡ xs >>= return
right-ret [] = refl
right-ret (x ∷ xs) = cong (_∷_ x) (right-ret xs)
>>=-rdist :
∀ {a b}{A : Set a}{B : Set b}
xs ys (f : A → List B)
→ ((xs ++ ys) >>= f) ≡ (xs >>= f) ++ (ys >>= f)
>>=-rdist [] ys f = refl
>>=-rdist (x ∷ xs) ys f rewrite
LM.assoc (f x) (xs >>= f) (ys >>= f)
| >>=-rdist xs ys f
= refl
assoc :
∀ {a b c}{A : Set a}{B : Set b}{C : Set c}
(xs : List A)
(f : A → List B)
(g : B → List C) →
((xs >>= f) >>= g) ≡ (xs >>= λ x → f x >>= g)
assoc [] f g = refl
assoc (x ∷ xs) f g rewrite
sym $ assoc xs f g
| sym $ >>=-rdist (f x) (xs >>= f) g
= refl
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment