Built with Alectryon, running Coq+SerAPI v8.16.0+0.16.3. Bubbles () indicate interactive fragments: hover for details, tap to reveal contents. Use Ctrl+↑Ctrl+↓ to navigate, Ctrl+🖱️ to focus. On Mac, use ⌘ instead of Ctrl.
From Tealeaves Require Export
Classes.Kleisli.Monad
Functors.Compose.(** * State Monad *)(**********************************************************************)InductiveState (SA: Type) :=
| mkState: (S -> S * A) -> State S A.Arguments mkState {S A}%type_scope _.Sectionstate_monad.Context
{S: Type}.DefinitionrunState {A}: State S A -> S -> S * A :=
funsts => match st with
| mkState run => run s
end.DefinitionrunStateValue {A}: State S A -> S -> A :=
funsts => snd (runState st s).DefinitionrunStateState {A}: State S A -> S -> S :=
funsts => fst (runState st s).(** ** Functor Instance *)(********************************************************************)#[export] InstanceMap_State: Map (State S) :=
funAB (f: A -> B) (st: State S A) =>
match st with
| mkState r =>
mkState (funs => match (r s) with (s', a) => (s', f a) end)
end.
S: Type
Functor (State S)
S: Type
Functor (State S)
S: Type
forallA : Type, map id = id
S: Type
forall (ABC : Type) (f : A -> B) (g : B -> C),
map g ∘ map f = map (g ∘ f)
S: Type
forallA : Type, map id = id
S, A: Type
map id = id
S, A: Type st: S -> S * A
map id (mkState st) = id (mkState st)
S, A: Type st: S -> S * A
map (funx : A => x) (mkState st) = mkState st
S, A: Type st: S -> S * A
mkState (funs : S => let (s', a) := st s in (s', a)) =
mkState st
S, A: Type st: S -> S * A
(funs : S => let (s', a) := st s in (s', a)) = st
S, A: Type st: S -> S * A s: S
(let (s', a) := st s in (s', a)) = st s
nowdestruct (st s).
S: Type
forall (ABC : Type) (f : A -> B) (g : B -> C),
map g ∘ map f = map (g ∘ f)
S, A, B, C: Type f: A -> B g: B -> C
map g ∘ map f = map (g ∘ f)
S, A, B, C: Type f: A -> B g: B -> C st: S -> S * A
S, A, B, C: Type f: A -> B g: B -> C st: S -> S * A
map g (map f (mkState st)) = map (g ○ f) (mkState st)
S, A, B, C: Type f: A -> B g: B -> C st: S -> S * A
mkState
(funs : S =>
let (s', a) := let (s', a) := st s in (s', f a) in
(s', g a)) =
mkState
(funs : S => let (s', a) := st s in (s', g (f a)))
S, A, B, C: Type f: A -> B g: B -> C st: S -> S * A
(funs : S =>
let (s', a) := let (s', a) := st s in (s', f a) in
(s', g a)) =
(funs : S => let (s', a) := st s in (s', g (f a)))
S, A, B, C: Type f: A -> B g: B -> C st: S -> S * A s: S
(let (s', a) := let (s', a) := st s in (s', f a) in
(s', g a)) = (let (s', a) := st s in (s', g (f a)))
nowdestruct (st s).Qed.(** ** Monad Instance (Kleisli) *)(********************************************************************)#[export] InstanceReturn_State: Return (State S) :=
funA (a: A) => mkState (funs => (s, a)).#[export] InstanceBind_State: Bind (State S) (State S) :=
funAB (f: A -> State S B) (st1: State S A) =>
match st1 with
| mkState action =>
mkState
(funs => match (action s) with
(s', a) => runState (f a) s'
end)
end.
S: Type
Monad (State S)
S: Type
Monad (State S)
S: Type
forall (AB : Type) (f : A -> State S B),
bind f ∘ ret = f
S: Type
RightPreModule (State S) (State S)
S: Type
forall (AB : Type) (f : A -> State S B),
bind f ∘ ret = f
S, A, B: Type f: A -> State S B
bind f ∘ ret = f
S, A, B: Type f: A -> State S B a: A
(bind f ∘ ret) a = f a
S, A, B: Type f: A -> State S B a: A
mkState
(funs : S =>
match f a with
| mkState run => run s
end) = f a
nowdestruct (f a).
S: Type
RightPreModule (State S) (State S)
S: Type
forallA : Type, bind ret = id
S: Type
forall (ABC : Type) (g : B -> State S C)
(f : A -> State S B),
bind g ∘ bind f = bind (kc g f)
S: Type
forallA : Type, bind ret = id
S, A: Type
bind ret = id
S, A: Type s: State S A
bind ret s = id s
S, A: Type s: State S A
match s with
| mkState action =>
mkState
(funs : S => let (s', a) := action s in (s', a))
end = s
S, A: Type p: S -> S * A
mkState (funs : S => let (s', a) := p s in (s', a)) =
mkState p
S, A: Type p: S -> S * A
(funs : S => let (s', a) := p s in (s', a)) = p
S, A: Type p: S -> S * A s: S
(let (s', a) := p s in (s', a)) = p s
nowdestruct (p s).
S: Type
forall (ABC : Type) (g : B -> State S C)
(f : A -> State S B),
bind g ∘ bind f = bind (kc g f)
S, A, B, C: Type g: B -> State S C f: A -> State S B
bind g ∘ bind f = bind (kc g f)
S, A, B, C: Type g: B -> State S C f: A -> State S B s: State S A
(bind g ∘ bind f) s = bind (kc g f) s
S, A, B, C: Type g: B -> State S C f: A -> State S B s: State S A
matchmatch s with
| mkState action =>
mkState
(funs : S =>
let (s', a) := action s inmatch f a with
| mkState run => run s'
end)
endwith
| mkState action =>
mkState
(funs : S =>
let (s', a) := action s inmatch g a with
| mkState run => run s'
end)
end =
match s with
| mkState action =>
mkState
(funs : S =>
let (s', a) := action s inmatchmatch f a with
| mkState action0 =>
mkState
(funs0 : S =>
let (s'0, a0) := action0 s0 inmatch g a0 with
| mkState run => run s'0
end)
endwith
| mkState run => run s'
end)
end
S, A, B, C: Type g: B -> State S C f: A -> State S B p: S -> S * A
mkState
(funs : S =>
let (s', a) :=
let (s', a) := p s inmatch f a with
| mkState run => run s'
endinmatch g a with
| mkState run => run s'
end) =
mkState
(funs : S =>
let (s', a) := p s inmatchmatch f a with
| mkState action =>
mkState
(funs0 : S =>
let (s'0, a0) := action s0 inmatch g a0 with
| mkState run => run s'0
end)
endwith
| mkState run => run s'
end)
S, A, B, C: Type g: B -> State S C f: A -> State S B p: S -> S * A
(funs : S =>
let (s', a) :=
let (s', a) := p s inmatch f a with
| mkState run => run s'
endinmatch g a with
| mkState run => run s'
end) =
(funs : S =>
let (s', a) := p s inmatchmatch f a with
| mkState action =>
mkState
(funs0 : S =>
let (s'0, a0) := action s0 inmatch g a0 with
| mkState run => run s'0
end)
endwith
| mkState run => run s'
end)
S, A, B, C: Type g: B -> State S C f: A -> State S B p: S -> S * A s: S
(let (s', a) :=
let (s', a) := p s inmatch f a with
| mkState run => run s'
endinmatch g a with
| mkState run => run s'
end) =
(let (s', a) := p s inmatchmatch f a with
| mkState action =>
mkState
(funs : S =>
let (s'0, a0) := action s inmatch g a0 with
| mkState run => run s'0
end)
endwith
| mkState run => run s'
end)
S, A, B, C: Type g: B -> State S C f: A -> State S B p: S -> S * A s, s0: S a: A
(let (s', a) :=
match f a with
| mkState run => run s0
endinmatch g a with
| mkState run => run s'
end) =
matchmatch f a with
| mkState action =>
mkState
(funs : S =>
let (s', a) := action s inmatch g a with
| mkState run => run s'
end)
endwith
| mkState run => run s0
end
nowdestruct (f a).Qed.(** ** Monad Instance (Categorical) *)(********************************************************************)#[local] InstanceJoin_State: Monad.Join (State S) :=
funA (st: State S (State S A)) =>
match st with
| mkState r =>
mkState (funs => match (r s) with (s', st') => runState st' s' end)
end.
S: Type
Natural (@ret (State S) Return_State)
S: Type
Natural (@ret (State S) Return_State)
S: Type
forall (AB : Type) (f : A -> B),
map f ∘ ret = ret ∘ map f
reflexivity.Qed.
S: Type
Natural (@Monad.join (State S) Join_State)
S: Type
Natural (@Monad.join (State S) Join_State)
S: Type
forall (AB : Type) (f : A -> B),
map f ∘ Monad.join = Monad.join ∘ map f
mkState
(funs : S =>
let (s', a) :=
let (s', st') := st s in runState st' s' in
(s', f a)) =
mkState
(funs : S =>
let (s', st') :=
let (s', a) := st s in (s', map f a) in
runState st' s')
S, A, B: Type f: A -> B st: S -> S * State S A
(funs : S =>
let (s', a) :=
let (s', st') := st s in runState st' s' in
(s', f a)) =
(funs : S =>
let (s', st') :=
let (s', a) := st s in (s', map f a) in
runState st' s')
S, A, B: Type f: A -> B st: S -> S * State S A s: S
(let (s', a) :=
let (s', st') := st s in runState st' s' in
(s', f a)) =
(let (s', st') :=
let (s', a) := st s in (s', map f a) in
runState st' s')
S, A, B: Type f: A -> B st: S -> S * State S A s, s0: S s1: State S A
(let (s', a) := runState s1 s0 in (s', f a)) =
runState (map f s1) s0
mkState
(funs : S =>
let (s', st') :=
let (s', st') := st s in runState st' s' in
runState st' s') =
mkState
(funs : S =>
let (s', st') :=
let (s', a) := st s in (s', Monad.join a) in
runState st' s')
S, A: Type st: S -> S * State S (State S A)
(funs : S =>
let (s', st') :=
let (s', st') := st s in runState st' s' in
runState st' s') =
(funs : S =>
let (s', st') :=
let (s', a) := st s in (s', Monad.join a) in
runState st' s')
S, A: Type st: S -> S * State S (State S A) s: S
(let (s', st') :=
let (s', st') := st s in runState st' s' in
runState st' s') =
(let (s', st') :=
let (s', a) := st s in (s', Monad.join a) in
runState st' s')
S, A: Type st: S -> S * State S (State S A) s, s0: S s1: State S (State S A)