monad
本頁內容尚未翻譯。
IsMonadAssoc
Section titled “IsMonadAssoc”def IsMonadAssoc (M : Type -> Type) (bnd : (A : Type) -> (B : Type) -> M A -> (A -> M B) -> M B) : Type 1The monad associativity law for bind: bind (bind m f) g = bind m (fun a => bind (f a) g).
IsMonad
Section titled “IsMonad”def IsMonad (M : Type -> Type) (ret : (A : Type) -> A -> M A) (bnd : (A : Type) -> (B : Type) -> M A -> (A -> M B) -> M B) : Type 1The type saying return and bind make M a lawful monad: left identity, right identity and associativity.
kleisli
Section titled “kleisli”def kleisli (M : Type -> Type) (bnd : (A : Type) -> (B : Type) -> M A -> (A -> M B) -> M B) (A : Type) (B : Type) (C : Type) (f : A -> M B) (g : B -> M C) (a : A) : M CKleisli composition for a monad given by its bind: run f, then feed the result to g.
pureOption
Section titled “pureOption”def pureOption (A : Type) (a : A) : Option AThe monadic return for Maybe with an explicit type argument: wraps a value in just.
bindOptionFull
Section titled “bindOptionFull”def bindOptionFull (A : Type) (B : Type) (mx : Option A) (f : A -> Option B) : Option BThe monadic bind for Maybe with explicit type arguments: continues with the value, or propagates nothing.
joinOption
Section titled “joinOption”def joinOption (A : Type) (mmx : Option (Option A)) : Option AFlattens a nested Maybe: just (just a) becomes just a, anything else nothing.
bindOptionLeftId
Section titled “bindOptionLeftId”def bindOptionLeftId (A : Type) (B : Type) (a : A) (f : A -> Option B) : bindOptionFull A B (pureOption A a) f ≡ f aThe Maybe monad satisfies left identity: bind (pure a) f = f a.
bindOptionRightId
Section titled “bindOptionRightId”def bindOptionRightId (A : Type) (mx : Option A) : bindOptionFull A A mx (pureOption A) ≡ mxThe Maybe monad satisfies right identity: bind m pure = m.
bindOptionAssoc
Section titled “bindOptionAssoc”def bindOptionAssoc (A : Type) (B : Type) (C : Type) (mx : Option A) (f : A -> Option B) (g : B -> Option C) : bindOptionFull B C (bindOptionFull A B mx f) g ≡ bindOptionFull A C mx (fun (a : A) => bindOptionFull B C (f a) g)The Maybe monad’s bind is associative: bind (bind m f) g = bind m (fun a => bind (f a) g).
optionIsMonad
Section titled “optionIsMonad”def optionIsMonad : IsMonad Option pureOption bindOptionFullMaybe with pureMaybe and bindMaybeFull is a lawful monad.
pureList
Section titled “pureList”def pureList (A : Type) (a : A) : List AThe monadic return for List: the singleton list.
bindList
Section titled “bindList”def bindList (A : Type) (B : Type) (xs : List A) (f : A -> List B) : List BThe monadic bind for List (flatMap): applies f to each element and concatenates the results.
joinList
Section titled “joinList”def joinList (A : Type) (xss : List (List A)) : List AFlattens a list of lists by concatenation.
bindListLeftId
Section titled “bindListLeftId”def bindListLeftId (A : Type) (B : Type) (a : A) (f : A -> List B) : bindList A B (pureList A a) f ≡ f aThe List monad satisfies left identity: bind [a] f = f a.
bindListRightId
Section titled “bindListRightId”def bindListRightId (A : Type) (xs : List A) : bindList A A xs (pureList A) ≡ xsThe List monad satisfies right identity: bind xs pure = xs.
bindListAppend
Section titled “bindListAppend”def bindListAppend (A : Type) (B : Type) (xs : List A) (ys : List A) (f : A -> List B) : bindList A B (append xs ys) f ≡ append (bindList A B xs f) (bindList A B ys f)Binding distributes over append: bind (xs ++ ys) f = bind xs f ++ bind ys f.
listAssoc
Section titled “listAssoc”def listAssoc (A : Type) (B : Type) (C : Type) (xs : List A) (f : A -> List B) (g : B -> List C) : bindList B C (bindList A B xs f) g ≡ bindList A C xs (fun (a : A) => bindList B C (f a) g)The List monad’s bind is associative: bind (bind xs f) g = bind xs (fun a => bind (f a) g).
listIsMonad
Section titled “listIsMonad”def listIsMonad : IsMonad List pureList bindListList with pureList and bindList is a lawful monad.
kleisliOptionLeftId
Section titled “kleisliOptionLeftId”def kleisliOptionLeftId (A : Type) (B : Type) (f : A -> Option B) (a : A) : kleisliOption A A B (pureOption A) f a ≡ f aPure is a left identity for Kleisli composition in Maybe.
kleisliOptionRightId
Section titled “kleisliOptionRightId”def kleisliOptionRightId (A : Type) (B : Type) (f : A -> Option B) (a : A) : kleisliOption A B B f (pureOption B) a ≡ f aPure is a right identity for Kleisli composition in Maybe.
kleisliOptionAssoc
Section titled “kleisliOptionAssoc”def kleisliOptionAssoc (A : Type) (B : Type) (C : Type) (D : Type) (f : A -> Option B) (g : B -> Option C) (h : C -> Option D) (a : A) : kleisliOption A C D (fun (x : A) => kleisliOption A B C f g x) h a ≡ kleisliOption A B D f (fun (b : B) => kleisliOption B C D g h b) aKleisli composition in Maybe is associative: (f >=> g) >=> h = f >=> (g >=> h).
kleisliListLeftId
Section titled “kleisliListLeftId”def kleisliListLeftId (A : Type) (B : Type) (f : A -> List B) (a : A) : kleisliList A A B (pureList A) f a ≡ f aPure is a left identity for Kleisli composition in List.
kleisliListRightId
Section titled “kleisliListRightId”def kleisliListRightId (A : Type) (B : Type) (f : A -> List B) (a : A) : kleisliList A B B f (pureList B) a ≡ f aPure is a right identity for Kleisli composition in List.
joinOptionNone
Section titled “joinOptionNone”def joinOptionNone (A : Type) : joinOption A (none : Option (Option A)) ≡ (none : Option A)Joining nothing gives nothing.
joinOptionSomeNone
Section titled “joinOptionSomeNone”def joinOptionSomeNone (A : Type) : joinOption A (some (none : Option A)) ≡ (none : Option A)Joining just nothing gives nothing.
joinOptionSomeSome
Section titled “joinOptionSomeSome”def joinOptionSomeSome (A : Type) (a : A) : joinOption A (some (some a)) ≡ some aJoining just (just a) gives just a.
joinOptionPure
Section titled “joinOptionPure”def joinOptionPure (A : Type) (mx : Option A) : joinOption A (pureOption (Option A) mx) ≡ mxJoin after pure is the identity on Maybe: join (pure m) = m.
joinOptionFmapPureSome
Section titled “joinOptionFmapPureSome”def joinOptionFmapPureSome (A : Type) (x : A) : joinOption A (some (pureOption A x)) ≡ some xJoin after mapping pure is the identity, just case: join (just (pure x)) = just x.
joinListNil
Section titled “joinListNil”def joinListNil (A : Type) : joinList A (nil : List (List A)) ≡ (nil : List A)Flattening the empty list of lists gives the empty list.
joinListSingleton
Section titled “joinListSingleton”def joinListSingleton (A : Type) (xs : List A) : joinList A (cons xs nil) ≡ xsFlattening a one-element list of lists gives that element.
joinListConsNil
Section titled “joinListConsNil”def joinListConsNil (A : Type) (xss : List (List A)) : joinList A (cons nil xss) ≡ joinList A xssFlattening skips a leading empty list: join ([] :: xss) = join xss.
mapViaBind
Section titled “mapViaBind”def mapViaBind (A : Type) (B : Type) (f : A -> B) (mx : Option A) : Option BThe functor map for Maybe expressed through bind and pure.
mapViaBindNone
Section titled “mapViaBindNone”def mapViaBindNone (A : Type) (B : Type) (f : A -> B) : mapViaBind A B f (none : Option A) ≡ (none : Option B)Mapping via bind over nothing gives nothing.
mapViaBindSome
Section titled “mapViaBindSome”def mapViaBindSome (A : Type) (B : Type) (f : A -> B) (x : A) : mapViaBind A B f (some x) ≡ some (f x)Mapping via bind over just x gives just (f x).
guardOption
Section titled “guardOption”def guardOption (b : Bool) : Option UnitGuard for Maybe: just unit when the condition is true, nothing otherwise.
filterOption
Section titled “filterOption”def filterOption (A : Type) (p : A -> Bool) (mx : Option A) : Option AKeeps the contained value only when it satisfies the Boolean predicate p, otherwise gives nothing.
mapViaBindList
Section titled “mapViaBindList”def mapViaBindList (A : Type) (B : Type) (f : A -> B) (xs : List A) : List BThe functor map for List expressed through bind and pure.
mapViaBindListNil
Section titled “mapViaBindListNil”def mapViaBindListNil (A : Type) (B : Type) (f : A -> B) : mapViaBindList A B f (nil : List A) ≡ (nil : List B)Mapping via bind over the empty list gives the empty list.
mapViaBindListCons
Section titled “mapViaBindListCons”def mapViaBindListCons (A : Type) (B : Type) (f : A -> B) (x : A) (xs : List A) : mapViaBindList A B f (cons x xs) ≡ cons (f x) (mapViaBindList A B f xs)Mapping via bind over a cons list maps the head and recurses: map f (x :: xs) = f x :: map f xs.
filterViaBind
Section titled “filterViaBind”def filterViaBind (A : Type) (p : A -> Bool) (xs : List A) : List AFilters a list by the Boolean predicate p, expressed through bind.
optionBindMapNone
Section titled “optionBindMapNone”def optionBindMapNone (A : Type) (B : Type) (f : A -> B) : bindOptionFull A B (none : Option A) (fun (a : A) => pureOption B (f a)) ≡ (none : Option B)Binding nothing with pure ∘ f gives nothing.
optionBindMapSome
Section titled “optionBindMapSome”def optionBindMapSome (A : Type) (B : Type) (f : A -> B) (x : A) : bindOptionFull A B (some x) (fun (a : A) => pureOption B (f a)) ≡ some (f x)Binding just x with pure ∘ f gives just (f x).
optionBindFmapNone
Section titled “optionBindFmapNone”def optionBindFmapNone (A : Type) (B : Type) (C : Type) (f : A -> B) (g : B -> Option C) : bindOptionFull B C (none : Option B) g ≡ (none : Option C)Binding nothing with any continuation gives nothing.
optionBindFmapSome
Section titled “optionBindFmapSome”def optionBindFmapSome (A : Type) (B : Type) (C : Type) (f : A -> B) (g : B -> Option C) (x : A) : bindOptionFull B C (some (f x)) g ≡ g (f x)Binding just (f x) with g gives g (f x).
optionPureNatWitness
Section titled “optionPureNatWitness”def optionPureNatWitness (A : Type) (B : Type) (f : A -> B) (a : A) : some (f a) ≡ pureOption B (f a)just (f a) is pure (f a), the computation behind naturality of pure for Maybe.
optionJoinNatNone
Section titled “optionJoinNatNone”def optionJoinNatNone (A : Type) (B : Type) (f : A -> B) : joinOption B (none : Option (Option B)) ≡ (none : Option B)Joining nothing gives nothing (a case of naturality of join for Maybe).
optionJoinNatSomeNone
Section titled “optionJoinNatSomeNone”def optionJoinNatSomeNone (A : Type) (B : Type) (f : A -> B) : joinOption B (some (none : Option B)) ≡ (none : Option B)Joining just nothing gives nothing (a case of naturality of join for Maybe).
optionJoinNatSomeSome
Section titled “optionJoinNatSomeSome”def optionJoinNatSomeSome (A : Type) (B : Type) (f : A -> B) (x : A) : joinOption B (some (some (f x))) ≡ some (f x)Joining just (just (f x)) gives just (f x) (a case of naturality of join for Maybe).
strengthOption
Section titled “strengthOption”def strengthOption (A : Type) (B : Type) (p : (a : A) × Option B) : Option ((a : A) × B)The strength of Maybe: moves a pair whose second component is a Maybe inside the Maybe.
strengthList
Section titled “strengthList”def strengthList (A : Type) (B : Type) (a : A) (xs : List B) : List ((a2 : A) × B)The strength of List: pairs a fixed value a with every element of the list.
strengthListNil
Section titled “strengthListNil”def strengthListNil (A : Type) (B : Type) (a : A) : strengthList A B a (nil : List B) ≡ (nil : List ((a2 : A) × B))The strength of List on the empty list is the empty list.
liftM2Option
Section titled “liftM2Option”def liftM2Option (A : Type) (B : Type) (C : Type) (f : A -> B -> C) (mx : Option A) (my : Option B) : Option CLifts a binary function to Maybe: applies f when both arguments are present.
liftM2List
Section titled “liftM2List”def liftM2List (A : Type) (B : Type) (C : Type) (f : A -> B -> C) (xs : List A) (ys : List B) : List CLifts a binary function to List: applies f to every pair from the Cartesian product.
sequenceOption
Section titled “sequenceOption”def sequenceOption (A : Type) (xs : List (Option A)) : Option (List A)Turns a list of Maybes into a Maybe list: just the values if all are present, otherwise nothing.
traverseOption
Section titled “traverseOption”def traverseOption (A : Type) (B : Type) (f : A -> Option B) (xs : List A) : Option (List B)Applies a partial function to every element: just the results if all succeed, otherwise nothing.
optionToListPure
Section titled “optionToListPure”def optionToListPure (A : Type) (a : A) : Option.toList A (pureOption A a) ≡ pureList A aConverting Maybe to List sends pure to pure: maybeToList (pure a) = [a].
optionToListBindNone
Section titled “optionToListBindNone”def optionToListBindNone (A : Type) (B : Type) (f : A -> Option B) : Option.toList B (bindOptionFull A B (none : Option A) f) ≡ bindList A B (Option.toList A (none : Option A)) (fun (a : A) => Option.toList B (f a))Converting Maybe to List preserves bind, nothing case.
optionToListBindSome
Section titled “optionToListBindSome”def optionToListBindSome (A : Type) (B : Type) (a : A) (f : A -> Option B) : Option.toList B (bindOptionFull A B (some a) f) ≡ bindList A B (Option.toList A (some a)) (fun (x : A) => Option.toList B (f x))Converting Maybe to List preserves bind, just case.
listToOption
Section titled “listToOption”def listToOption (A : Type) (xs : List A) : Option AThe head of a list as a Maybe: nothing for the empty list.
orElseOption
Section titled “orElseOption”def orElseOption (A : Type) (mx : Option A) (my : Option A) : Option ALeft-biased choice for Maybe: the first argument if present, otherwise the second.
orElseOptionNoneL
Section titled “orElseOptionNoneL”def orElseOptionNoneL (A : Type) (my : Option A) : orElseOption A (none : Option A) my ≡ mynothing is a left identity for orElseMaybe.
orElseOptionSomeL
Section titled “orElseOptionSomeL”def orElseOptionSomeL (A : Type) (x : A) (my : Option A) : orElseOption A (some x) my ≡ some xorElseMaybe returns a present left argument: orElse (just x) m = just x.
orElseOptionAssoc
Section titled “orElseOptionAssoc”def orElseOptionAssoc (A : Type) (mx : Option A) (my : Option A) (mz : Option A) : orElseOption A (orElseOption A mx my) mz ≡ orElseOption A mx (orElseOption A my mz)orElseMaybe is associative.
orElseOptionNoneR
Section titled “orElseOptionNoneR”def orElseOptionNoneR (A : Type) (mx : Option A) : orElseOption A mx (none : Option A) ≡ mxnothing is a right identity for orElseMaybe.
apOption
Section titled “apOption”def apOption (A : Type) (B : Type) (mf : Option (A -> B)) (mx : Option A) : Option BThe applicative apply for Maybe: applies a wrapped function to a wrapped value, nothing if either is missing.
apOptionId
Section titled “apOptionId”def apOptionId (A : Type) (mx : Option A) : apOption A A (some (fun (a : A) => a)) mx ≡ mxThe Maybe applicative satisfies the identity law: cong (just id) m = m.
voidOption
Section titled “voidOption”def voidOption (A : Type) (mx : Option A) : Option UnitDiscards the contained value, keeping only whether it is present.
whenOption
Section titled “whenOption”def whenOption (b : Bool) : Option Unitjust unit when the condition is true, nothing otherwise.
unlessOption
Section titled “unlessOption”def unlessOption (b : Bool) : Option Unitjust unit when the condition is false, nothing otherwise.
safeHead
Section titled “safeHead”def safeHead : List Nat -> Option NatThe head of a list of naturals, or nothing for the empty list.
safePred
Section titled “safePred”def safePred : Nat -> Option NatThe predecessor of a natural number, or nothing for zero.
kleisli_head_pred
Section titled “kleisli_head_pred”def kleisli_head_pred (xs : List Nat) : Option NatExample Kleisli composite in Maybe: the predecessor of the head of a list, if both exist.
kleisliOptionPureLeft
Section titled “kleisliOptionPureLeft”def kleisliOptionPureLeft (f : Nat -> Option Nat) (n : Nat) : kleisliOption Nat Nat Nat (pureOption Nat) f n ≡ f nPure is a left identity for Kleisli composition in Maybe (on Nat).
kleisliOptionPureRight
Section titled “kleisliOptionPureRight”def kleisliOptionPureRight (f : Nat -> Option Nat) (n : Nat) : kleisliOption Nat Nat Nat f (pureOption Nat) n ≡ f nPure is a right identity for Kleisli composition in Maybe (on Nat).
duplicateList
Section titled “duplicateList”def duplicateList (n : Nat) : List NatThe two-element list [n, n].
succList
Section titled “succList”def succList (n : Nat) : List NatThe singleton list [n + 1].
kleisli_dup_succ
Section titled “kleisli_dup_succ”def kleisli_dup_succ (n : Nat) : List NatExample Kleisli composite in List: duplicate n, then take each successor, giving [n + 1, n + 1].
joinMapNone
Section titled “joinMapNone”def joinMapNone (A : Type) (B : Type) (f : A -> Option B) : joinOption B (mapViaBind A (Option B) f (none : Option A)) ≡ bindOptionFull A B (none : Option A) fBind is join after map for Maybe, nothing case.
joinMapSome
Section titled “joinMapSome”def joinMapSome (A : Type) (B : Type) (f : A -> Option B) (a : A) : joinOption B (mapViaBind A (Option B) f (some a)) ≡ bindOptionFull A B (some a) fBind is join after map for Maybe, just case.
joinMapNil
Section titled “joinMapNil”def joinMapNil (A : Type) (B : Type) (f : A -> List B) : joinList B (mapViaBindList A (List B) f (nil : List A)) ≡ bindList A B (nil : List A) fBind is join after map for List, empty-list case.
joinMapSingleton
Section titled “joinMapSingleton”def joinMapSingleton (A : Type) (B : Type) (f : A -> List B) (a : A) : joinList B (mapViaBindList A (List B) f (cons a (nil : List A))) ≡ append (f a) (nil : List B)Join after map on a singleton list [a] gives f a ++ [].
mapViaBindIdNone
Section titled “mapViaBindIdNone”def mapViaBindIdNone (A : Type) : mapViaBind A A (fun (x : A) => x) (none : Option A) ≡ (none : Option A)Mapping the identity via bind over nothing gives nothing.
mapViaBindIdSome
Section titled “mapViaBindIdSome”def mapViaBindIdSome (A : Type) (a : A) : mapViaBind A A (fun (x : A) => x) (some a) ≡ some aMapping the identity via bind over just a gives just a.
mapViaBindCompNone
Section titled “mapViaBindCompNone”def mapViaBindCompNone (A B C : Type) (f : B -> C) (g : A -> B) : mapViaBind B C f (mapViaBind A B g (none : Option A)) ≡ mapViaBind A C (fun (x : A) => f (g x)) (none : Option A)Mapping via bind respects composition, nothing case.
mapViaBindCompSome
Section titled “mapViaBindCompSome”def mapViaBindCompSome (A B C : Type) (f : B -> C) (g : A -> B) (a : A) : mapViaBind B C f (mapViaBind A B g (some a)) ≡ mapViaBind A C (fun (x : A) => f (g x)) (some a)Mapping via bind respects composition, just case.
filterOptionFalse
Section titled “filterOptionFalse”def filterOptionFalse (A : Type) (mx : Option A) : filterOption A (fun (_ : A) => false) mx ≡ (none : Option A)Filtering with the always-false predicate gives nothing.
filterOptionTrueSome
Section titled “filterOptionTrueSome”def filterOptionTrueSome (A : Type) (x : A) : filterOption A (fun (_ : A) => true) (some x) ≡ some xFiltering just x with the always-true predicate keeps it.
guardBindTrueF
Section titled “guardBindTrueF”def guardBindTrueF (B : Type) (f : Unit -> Option B) : bindOptionFull Unit B (guardOption true) f ≡ f unitBinding a true guard continues: bind (guard true) f = f unit.
guardBindFalseF
Section titled “guardBindFalseF”def guardBindFalseF (B : Type) (f : Unit -> Option B) : bindOptionFull Unit B (guardOption false) f ≡ (none : Option B)Binding a false guard fails: bind (guard false) f = nothing.
triple_pred
Section titled “triple_pred”def triple_pred (n : Nat) : Option NatExample bind chain: the predecessor taken three times, nothing if n < 3.
quadruplicate
Section titled “quadruplicate”def quadruplicate (n : Nat) : List NatExample bind chain: duplicating twice gives four copies of n.
double_succ_list
Section titled “double_succ_list”def double_succ_list (n : Nat) : List NatExample bind chain: the successor taken twice, as a singleton list [n + 2].