Coinductive formalization of unbounded computations. #
This file provides a Computation
type where Computation α
is the type of
unbounded computations returning α
.
Computation α
is the type of unbounded computations returning α
.
An element of Computation α
is an infinite sequence of Option α
such
that if f n = some a
for some n
then it is constantly some a
after that.
Equations
Instances For
pure a
is the computation that immediately terminates with result a
.
Equations
- Computation.pure a = ⟨Stream'.const (some a), ⋯⟩
Instances For
Equations
- Computation.instCoeTC = { coe := Computation.pure }
think c
is the computation that delays for one "tick" and then performs
computation c
.
Equations
- c.think = ⟨Stream'.cons none ↑c, ⋯⟩
Instances For
thinkN c n
is the computation that delays for n
ticks and then performs
computation c
.
Instances For
empty α
is the computation that never returns, an infinite sequence of
think
s.
Equations
- Computation.empty α = ⟨Stream'.const none, ⋯⟩
Instances For
Equations
- Computation.instInhabited = { default := Computation.empty α }
destruct c
is the destructor for Computation α
as a coinductive type.
It returns inl a
if c = pure a
and inr c'
if c = think c'
.
Instances For
run c
is an unsound meta function that runs c
to completion, possibly
resulting in an infinite loop in the VM.
Instances For
Recursion principle for computations, compare with List.recOn
.
Equations
Instances For
corec f b
is the corecursor for Computation α
as a coinductive type.
If f b = inl a
then corec f b = pure a
, and if f b = inl b'
then
corec f b = think (corec f b')
.
Equations
- Computation.corec f b = ⟨Stream'.corec' (Computation.Corec.f f) (Sum.inr b), ⋯⟩
Instances For
left map of ⊕
Equations
- Computation.lmap f (Sum.inl a) = Sum.inl (f a)
- Computation.lmap f (Sum.inr val) = Sum.inr val
Instances For
right map of ⊕
Equations
- Computation.rmap f (Sum.inl a) = Sum.inl a
- Computation.rmap f (Sum.inr val) = Sum.inr (f val)
Instances For
Bisimilarity over a sum of Computation
s
Equations
- Computation.BisimO R (Sum.inl a) (Sum.inl a') = (a = a')
- Computation.BisimO R (Sum.inr s) (Sum.inr s') = R s s'
- Computation.BisimO R x✝¹ x✝ = False
Instances For
Attribute expressing bisimilarity over two Computation
s
Equations
- Computation.IsBisimulation R = ∀ ⦃s₁ s₂ : Computation α⦄, R s₁ s₂ → Computation.BisimO R s₁.destruct s₂.destruct
Instances For
Assertion that a Computation
limits to a given value
Instances For
Equations
- Computation.instMembership = { mem := Computation.Mem }
Terminates s
asserts that the computation s
eventually terminates with some value.
- term : ∃ (a : α), a ∈ s
assertion that there is some term
a
such that theComputation
terminates
Instances
Promises s a
, or s ~> a
, asserts that although the computation s
may not terminate, if it does, then the result is a
.
Instances For
Promises s a
, or s ~> a
, asserts that although the computation s
may not terminate, if it does, then the result is a
.
Equations
- Computation.«term_~>_» = Lean.ParserDescr.trailingNode `Computation.«term_~>_» 50 50 (Lean.ParserDescr.binary `andthen (Lean.ParserDescr.symbol " ~> ") (Lean.ParserDescr.cat `term 51))
Instances For
length s
gets the number of steps of a terminating computation
Instances For
get s
returns the result of a terminating computation
Instances For
Results s a n
completely characterizes a terminating computation:
it asserts that s
terminates after exactly n
steps, with result a
.
Instances For
Recursor based on membership
Equations
- Computation.memRecOn M h1 h2 = ⋯.mpr (⋯.mpr (Nat.recAux h1 (fun (n : ℕ) (IH : C ((Computation.pure a).thinkN n)) => h2 ((Computation.pure a).thinkN n) IH) s.length))
Instances For
Recursor based on assertion of Terminates
Equations
- s.terminatesRecOn h1 h2 = Computation.memRecOn ⋯ (h1 s.get) h2
Instances For
Map a function on the result of a computation.
Equations
- Computation.map f ⟨s, al⟩ = ⟨Stream'.map (fun (o : Option α) => Option.casesOn o none (some ∘ f)) s, ⋯⟩
Instances For
bind over a Sum
of Computation
Equations
- Computation.Bind.g (Sum.inl a) = Sum.inl a
- Computation.Bind.g (Sum.inr ca) = Sum.inr (Sum.inr ca)
Instances For
bind over a function mapping α
to a Computation
Equations
- Computation.Bind.f f (Sum.inl ca) = match ca.destruct with | Sum.inl a => Computation.Bind.g (f a).destruct | Sum.inr ca' => Sum.inr (Sum.inl ca')
- Computation.Bind.f f (Sum.inr cb) = Computation.Bind.g cb.destruct
Instances For
Compose two computations into a monadic bind
operation.
Equations
- c.bind f = Computation.corec (Computation.Bind.f f) (Sum.inl c)
Instances For
Equations
- Computation.instBind = { bind := @Computation.bind }
Flatten a computation of computations into a single computation.
Instances For
c₁ <|> c₂
calculates c₁
and c₂
simultaneously, returning
the first one that gives a result.
Equations
- One or more equations did not get rendered due to their size.
Instances For
c₁ ~ c₂
asserts that c₁
and c₂
either both terminate with the same result,
or both loop forever.
Instances For
equivalence relation for computations
Equations
- Computation.«term_~_» = Lean.ParserDescr.trailingNode `Computation.«term_~_» 50 50 (Lean.ParserDescr.binary `andthen (Lean.ParserDescr.symbol " ~ ") (Lean.ParserDescr.cat `term 51))
Instances For
LiftRel R ca cb
is a generalization of Equiv
to relations other than
equality. It asserts that if ca
terminates with a
, then cb
terminates with
some b
such that R a b
, and if cb
terminates with b
then ca
terminates
with some a
such that R a b
.
Equations
Instances For
Alternate definition of LiftRel
over relations between Computation
s
Equations
- Computation.LiftRelAux R C (Sum.inl a) (Sum.inl b) = R a b
- Computation.LiftRelAux R C (Sum.inl a) (Sum.inr cb) = ∃ (b : β), b ∈ cb ∧ R a b
- Computation.LiftRelAux R C (Sum.inr ca) (Sum.inl b) = ∃ (a : α), a ∈ ca ∧ R a b
- Computation.LiftRelAux R C (Sum.inr ca) (Sum.inr cb) = C ca cb