-
Notifications
You must be signed in to change notification settings - Fork 19
/
maybe.agda
43 lines (32 loc) · 1.31 KB
/
maybe.agda
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
module maybe where
open import level
open import eq
open import bool
----------------------------------------------------------------------
-- datatypes
----------------------------------------------------------------------
data maybe {ℓ}(A : Set ℓ) : Set ℓ where
nothing : maybe A
just : A → maybe A
{-# FOREIGN GHC type AgdaMaybe l = Maybe #-}
-- ^ Deal with maybe's level-polymorphism (the ℓ implicit parameter)
{-# COMPILE GHC maybe = data AgdaMaybe (Nothing | Just) #-}
----------------------------------------------------------------------
-- operations
----------------------------------------------------------------------
_≫=maybe_ : ∀ {ℓ}{A B : Set ℓ} → maybe A → (A → maybe B) → maybe B
nothing ≫=maybe f = nothing
(just x) ≫=maybe f = f x
return-maybe : ∀ {ℓ}{A : Set ℓ} → A → maybe A
return-maybe a = just a
down-≡ : ∀{ℓ}{A : Set ℓ}{a a' : A} → just a ≡ just a' → a ≡ a'
down-≡ refl = refl
isJust : ∀{ℓ}{A : Set ℓ} → maybe A → 𝔹
isJust nothing = ff
isJust (just _) = tt
maybe-extract : ∀{ℓ}{A : Set ℓ} → (x : maybe A) → isJust x ≡ tt → A
maybe-extract (just x) p = x
maybe-extract nothing ()
maybe-map : ∀{ℓ}{A B : Set ℓ} → (A → B) → maybe A → maybe B
maybe-map f (just x) = just (f x)
maybe-map f nothing = nothing