chars
int64
40
24k
content_type
stringclasses
9 values
id
stringlengths
13
64
source
stringclasses
5 values
subset
stringclasses
31 values
text
stringlengths
40
24k
314
leanexplore_declaration
leanexplore:386915
leanexplore_sqlite
leanexplore_declarations
/-- The joint distribution at `(m, c)` equals `msgDist m * ciphertextDist m c`. -/ theorem jointDist_eq (scheme : EncScheme M K C) (msgDist : PMF M) (m : M) (c : C) : scheme.jointDist msgDist (m, c) = msgDist m * scheme.ciphertextDist m c := PMFUtilities.bind_pair_apply msgDist scheme.ciphertextDist m c
330
leanexplore_declaration
leanexplore:386916
leanexplore_sqlite
leanexplore_declarations
/-- Summing the joint distribution over messages gives the marginal ciphertext distribution. -/ theorem jointDist_tsum_fst (scheme : EncScheme M K C) (msgDist : PMF M) (c : C) : ∑' m, scheme.jointDist msgDist (m, c) = scheme.marginalCiphertextDist msgDist c := PMFUtilities.bind_pair_tsum_fst msgDist scheme.cipher...
1,165
leanexplore_declaration
leanexplore:386917
leanexplore_sqlite
leanexplore_declarations
/-- Perfect secrecy is equivalent to message-ciphertext independence. The two formulations are related by multiplying/dividing by `marginal(c)`. -/ theorem perfectlySecret_iff_indep (scheme : EncScheme M K C) : scheme.PerfectlySecret ↔ ∀ (msgDist : PMF M) (m : M) (c : C), scheme.jointDist msgDist (m, c) =...
558
leanexplore_declaration
leanexplore:386918
leanexplore_sqlite
leanexplore_declarations
/-- Ciphertext indistinguishability implies message-ciphertext independence. -/ theorem indep_of_ciphertextIndist (scheme : EncScheme M K C) (h : scheme.CiphertextIndist) (msgDist : PMF M) (m : M) (c : C) : scheme.jointDist msgDist (m, c) = msgDist m * scheme.marginalCiphertextDist msgDist c := by r...
313
leanexplore_declaration
leanexplore:386919
leanexplore_sqlite
leanexplore_declarations
/-- Ciphertext indistinguishability implies perfect secrecy. -/ theorem perfectlySecret_of_ciphertextIndist (scheme : EncScheme M K C) (h : scheme.CiphertextIndist) : scheme.PerfectlySecret := (perfectlySecret_iff_indep scheme).mpr (fun msgDist m c => indep_of_ciphertextIndist scheme h msgDist m c)
798
leanexplore_declaration
leanexplore:386920
leanexplore_sqlite
leanexplore_declarations
/-- Perfect secrecy implies ciphertext indistinguishability. -/ theorem ciphertextIndist_of_perfectlySecret (scheme : EncScheme M K C) (h : scheme.PerfectlySecret) : scheme.CiphertextIndist := by classical rw [perfectlySecret_iff_indep] at h intro m₀ m₁; ext c have hs : ({m₀, m₁} : Finset M).Nonempty :=...
499
leanexplore_declaration
leanexplore:386921
leanexplore_sqlite
leanexplore_declarations
/-- If each message maps to a key that encrypts it to a common ciphertext, then the key assignment is injective (by correctness of decryption). -/ lemma encrypt_key_injective (scheme : EncScheme M K C) (f : M → K) (c₀ : C) (hf_mem : ∀ m, f m ∈ scheme.gen.support) (hf_enc : ∀ m, c₀ ∈ (scheme.enc (f m) m).sup...
766
leanexplore_declaration
leanexplore:386922
leanexplore_sqlite
leanexplore_declarations
/-- Perfect secrecy requires `|K| ≥ |M|` (Shannon's theorem). -/ theorem shannonKeySpace [Finite K] (scheme : EncScheme M K C) (h : scheme.PerfectlySecret) : Nat.card K ≥ Nat.card M := by classical have hci := ciphertextIndist_of_perfectlySecret scheme h by_cases hM : IsEmpty M; · simp obtain ⟨m₀⟩ := no...
299
leanexplore_declaration
leanexplore:386923
leanexplore_sqlite
leanexplore_declarations
/-- The one-time pad over `l`-bit strings. Encryption and decryption are XOR ([KatzLindell2020], Construction 2.9). -/ noncomputable def otp (l : ℕ) : EncScheme (BitVec l) (BitVec l) (BitVec l) := .ofPure (PMF.uniformOfFintype _) (· ^^^ ·) (· ^^^ ·) fun k m => by simp [← BitVec.xor_assoc]
371
leanexplore_declaration
leanexplore:386924
leanexplore_sqlite
leanexplore_declarations
/-- The one-time pad is perfectly secret ([KatzLindell2020], Theorem 2.10). -/ theorem otp_perfectlySecret (l : ℕ) : (otp l).PerfectlySecret := (EncScheme.perfectlySecret_iff_ciphertextIndist _).mpr fun m₀ m₁ => by simp only [EncScheme.ciphertextDist, otp] exact (OTP.otp_ciphertextDist_eq_uniform l m₀).trans ...
524
leanexplore_declaration
leanexplore:386925
leanexplore_sqlite
leanexplore_declarations
/-- Evaluating the "pairing" bind `(do let a ← p; return (a, ← f a))` at `(a, b)` gives the product `p a * f a b`. -/ theorem bind_pair_apply (p : PMF α) (f : α → PMF β) (a : α) (b : β) : (p.bind fun a' => (f a').bind fun b' => PMF.pure (a', b')) (a, b) = p a * f a b := by rw [PMF.bind_apply, tsum_eq_single a] ...
288
leanexplore_declaration
leanexplore:386926
leanexplore_sqlite
leanexplore_declarations
/-- Summing the pairing bind over the first component gives the marginal. -/ theorem bind_pair_tsum_fst (p : PMF α) (f : α → PMF β) (b : β) : ∑' a, (p.bind fun a' => (f a').bind fun b' => PMF.pure (a', b')) (a, b) = (p.bind f) b := by simp_rw [bind_pair_apply, PMF.bind_apply]
737
leanexplore_declaration
leanexplore:386927
leanexplore_sqlite
leanexplore_declarations
/-- Posterior probabilities `joint(a, b) / marginal(b)` sum to 1 when `b` is in the support of the marginal. -/ theorem posterior_hasSum (p : PMF α) (f : α → PMF β) (b : β) (hb : b ∈ (p.bind f).support) : HasSum (fun a => (p.bind fun a' => (f a').bind fun b' => PMF.pure (a', b')) (a, b) / (p.bind ...
386
leanexplore_declaration
leanexplore:386928
leanexplore_sqlite
leanexplore_declarations
/-- The posterior distribution `Pr[A = a | B = b]` as a `PMF`, given `a ← p`, `b ← f a`, and that `b` has positive marginal probability. -/ noncomputable def posteriorDist (p : PMF α) (f : α → PMF β) (b : β) (hb : b ∈ (p.bind f).support) : PMF α := ⟨fun a => (p.bind fun a' => (f a').bind fun b' => PMF.pure (a...
250
leanexplore_declaration
leanexplore:386929
leanexplore_sqlite
leanexplore_declarations
@[simp] theorem posteriorDist_apply (p : PMF α) (f : α → PMF β) (b : β) (hb : b ∈ (p.bind f).support) (a : α) : posteriorDist p f b hb a = (p.bind fun a' => (f a').bind fun b' => PMF.pure (a', b')) (a, b) / (p.bind f) b := rfl
629
leanexplore_declaration
leanexplore:386930
leanexplore_sqlite
leanexplore_declarations
/-- An infinite pigeonhole principle. -/ theorem infinite_pigeonhole_principle {X Y : Type*} [Finite Y] (f : X → Y) {s : Set X} (h_inf : s.Infinite) : ∃ y, ∃ t, t.Infinite ∧ t ⊆ s ∧ ∀ x ∈ t, f x = y := by have := h_inf.to_subtype obtain ⟨y, h_inf'⟩ := Finite.exists_infinite_fiber (s.restrict f) have h_inf_iff...
1,162
leanexplore_declaration
leanexplore:386931
leanexplore_sqlite
leanexplore_declarations
/-- If the edges of an infinite complete graph is assigned a finite number of colors, then there must exist a color `c` and an infinite set `s` of vertices such that the edge beteen any two vertices of `s` is assigned the same color `c`. -/ theorem infinite_graph_ramsey : ∃ c : Color, ∃ s : Set Vertex, s.Infinite ∧...
915
leanexplore_declaration
leanexplore:386932
leanexplore_sqlite
leanexplore_declarations
/-- The Free monad over a type constructor `F`. A `FreeM F a` is a tree of operations from the type constructor `F`, with leaves of type `a`. It has two constructors: `pure` for wrapping a value of type `a`, and `liftBind` for representing an operation from `F` followed by a continuation. This construction provides a...
91
leanexplore_declaration
leanexplore:386933
leanexplore_sqlite
leanexplore_declarations
/-- The action that does nothing and returns `a`. -/ | protected pure (a : α) : FreeM F α
234
leanexplore_declaration
leanexplore:386934
leanexplore_sqlite
leanexplore_declarations
/-- Invoke the operation `op` with contuation `cont`. Note that Lean's inductive types prevent us splitting this into separate bind and lift constructors. -/ | liftBind {ι : Type u} (op : F ι) (cont : ι → FreeM F α) : FreeM F α
45
leanexplore_declaration
leanexplore:386935
leanexplore_sqlite
leanexplore_declarations
instance : Pure (FreeM F) where pure := .pure
73
leanexplore_declaration
leanexplore:386936
leanexplore_sqlite
leanexplore_declarations
@[simp] theorem pure_eq_pure : (pure : α → FreeM F α) = FreeM.pure := rfl
217
leanexplore_declaration
leanexplore:386937
leanexplore_sqlite
leanexplore_declarations
/-- Bind operation for the `FreeM` monad. -/ protected def bind (x : FreeM F α) (f : α → FreeM F β) : FreeM F β := match x with | .pure a => f a | .liftBind op cont => .liftBind op fun z => FreeM.bind (cont z) f
252
leanexplore_declaration
leanexplore:386938
leanexplore_sqlite
leanexplore_declarations
protected theorem bind_assoc (x : FreeM F α) (f : α → FreeM F β) (g : β → FreeM F γ) : (x.bind f).bind g = x.bind (fun x => (f x).bind g) := by induction x with | pure a => rfl | liftBind op cont ih => simp [FreeM.bind] at * simp [ih]
45
leanexplore_declaration
leanexplore:386939
leanexplore_sqlite
leanexplore_declarations
instance : Bind (FreeM F) where bind := .bind
105
leanexplore_declaration
leanexplore:386940
leanexplore_sqlite
leanexplore_declarations
@[simp] theorem bind_eq_bind {α β : Type w} : Bind.bind = (FreeM.bind : FreeM F α → _ → FreeM F β) := rfl
190
leanexplore_declaration
leanexplore:386941
leanexplore_sqlite
leanexplore_declarations
/-- Map a function over a `FreeM` monad. -/ @[simp] def map (f : α → β) : FreeM F α → FreeM F β | .pure a => .pure (f a) | .liftBind op cont => .liftBind op fun z => FreeM.map f (cont z)
339
leanexplore_declaration
leanexplore:386942
leanexplore_sqlite
leanexplore_declarations
@[simp] theorem id_map : ∀ x : FreeM F α, map id x = x | .pure a => rfl | .liftBind op cont => by simp_all [map, id_map] theorem comp_map (h : β → γ) (g : α → β) : ∀ x : FreeM F α, map (h ∘ g) x = map h (map g x) | .pure a => rfl | .liftBind op cont => by simp_all [map, comp_map] instance : Functor (FreeM F) ...
163
leanexplore_declaration
leanexplore:386943
leanexplore_sqlite
leanexplore_declarations
theorem comp_map (h : β → γ) (g : α → β) : ∀ x : FreeM F α, map (h ∘ g) x = map h (map g x) | .pure a => rfl | .liftBind op cont => by simp_all [map, comp_map]
48
leanexplore_declaration
leanexplore:386944
leanexplore_sqlite
leanexplore_declarations
instance : Functor (FreeM F) where map := .map
101
leanexplore_declaration
leanexplore:386945
leanexplore_sqlite
leanexplore_declarations
@[simp] theorem map_eq_map {α β : Type w} : Functor.map = FreeM.map (F := F) (α := α) (β := β) := rfl
136
leanexplore_declaration
leanexplore:386946
leanexplore_sqlite
leanexplore_declarations
/-- Lift an operation from the effect signature `F` into the `FreeM F` monad. -/ def lift (op : F ι) : FreeM F ι := .liftBind op .pure
191
leanexplore_declaration
leanexplore:386947
leanexplore_sqlite
leanexplore_declarations
/-- Rewrite `lift` to the constructor form so that simplification stays in constructor normal form. -/ @[simp] lemma lift_def (op : F ι) : (lift op : FreeM F ι) = liftBind op .pure := rfl
137
leanexplore_declaration
leanexplore:386948
leanexplore_sqlite
leanexplore_declarations
@[simp] lemma map_lift (f : ι → α) (op : F ι) : map f (lift op : FreeM F ι) = liftBind op (fun z => (.pure (f z) : FreeM F α)) := rfl
154
leanexplore_declaration
leanexplore:386949
leanexplore_sqlite
leanexplore_declarations
/-- `.pure a` followed by `bind` collapses immediately. -/ @[simp] lemma pure_bind (a : α) (f : α → FreeM F β) : (.pure a : FreeM F α).bind f = f a := rfl
548
leanexplore_declaration
leanexplore:386950
leanexplore_sqlite
leanexplore_declarations
@[simp] lemma bind_pure : ∀ x : FreeM F α, x.bind (.pure) = x | .pure a => rfl | liftBind op k => by simp [FreeM.bind, bind_pure] @[simp] lemma bind_pure_comp (f : α → β) : ∀ x : FreeM F α, x.bind (.pure ∘ f) = map f x | .pure a => rfl | liftBind op k => by simp only [FreeM.bind, map, bind_pure_comp] /-- Coll...
413
leanexplore_declaration
leanexplore:386951
leanexplore_sqlite
leanexplore_declarations
@[simp] lemma bind_pure_comp (f : α → β) : ∀ x : FreeM F α, x.bind (.pure ∘ f) = map f x | .pure a => rfl | liftBind op k => by simp only [FreeM.bind, map, bind_pure_comp] /-- Collapse a `.bind` that follows a `liftBind` into a single `liftBind` -/ @[simp] lemma liftBind_bind (op : F ι) (cont : ι → FreeM F α) (f :...
236
leanexplore_declaration
leanexplore:386952
leanexplore_sqlite
leanexplore_declarations
/-- Collapse a `.bind` that follows a `liftBind` into a single `liftBind` -/ @[simp] lemma liftBind_bind (op : F ι) (cont : ι → FreeM F α) (f : α → FreeM F β) : (liftBind op cont).bind f = liftBind op fun x => (cont x).bind f := rfl
109
leanexplore_declaration
leanexplore:386953
leanexplore_sqlite
leanexplore_declarations
instance : LawfulFunctor (FreeM F) where map_const := rfl id_map := id_map comp_map _ _ := comp_map _ _
171
leanexplore_declaration
leanexplore:386955
leanexplore_sqlite
leanexplore_declarations
instance : LawfulMonad (FreeM F) := LawfulMonad.mk' (bind_pure_comp := bind_pure_comp) (id_map := id_map) (pure_bind := pure_bind) (bind_assoc := FreeM.bind_assoc)
544
leanexplore_declaration
leanexplore:386956
leanexplore_sqlite
leanexplore_declarations
/-- Interpret a `FreeM F` computation into any monad `m` by providing an interpretation function for the effect signature `F`. This function defines the *canonical interpreter* from the free monad `FreeM F` into the target monad `m`. It is the unique monad morphism that extends the effect handler `interp : ∀ {β}, F β ...
125
leanexplore_declaration
leanexplore:386957
leanexplore_sqlite
leanexplore_declarations
@[simp] lemma liftM_pure (interp : {ι : Type u} → F ι → m ι) (a : α) : (.pure a : FreeM F α).liftM interp = pure a := rfl
196
leanexplore_declaration
leanexplore:386958
leanexplore_sqlite
leanexplore_declarations
@[simp] lemma liftM_liftBind (interp : {ι : Type u} → F ι → m ι) (op : F β) (cont : β → FreeM F α) : (liftBind op cont).liftM interp = (do let b ← interp op; (cont b).liftM interp) := by rfl
193
leanexplore_declaration
leanexplore:386959
leanexplore_sqlite
leanexplore_declarations
lemma liftM_lift [LawfulMonad m] (interp : {ι : Type u} → F ι → m ι) (op : F β) : (lift op).liftM interp = interp op := by simp_rw [lift_def, liftM_liftBind, liftM_pure, _root_.bind_pure]
415
leanexplore_declaration
leanexplore:386960
leanexplore_sqlite
leanexplore_declarations
@[simp] lemma liftM_bind [LawfulMonad m] (interp : {ι : Type u} → F ι → m ι) (x : FreeM F α) (f : α → FreeM F β) : (x.bind f).liftM interp = (do let a ← x.liftM interp; (f a).liftM interp) := by induction x generalizing f with | pure a => simp only [pure_bind, liftM_pure, LawfulMonad.pure_bind] | liftBind...
263
leanexplore_declaration
leanexplore:386961
leanexplore_sqlite
leanexplore_declarations
@[simp] lemma liftM_map [LawfulMonad m] (interp : {ι : Type u} → F ι → m ι) (f : α → β) (x : FreeM F α) : (x.map f).liftM interp = f <$> x.liftM interp := by simp_rw [← bind_pure_comp, ← LawfulMonad.bind_pure_comp, liftM_bind, Function.comp, liftM_pure]
213
leanexplore_declaration
leanexplore:386962
leanexplore_sqlite
leanexplore_declarations
@[simp] lemma liftM_seq [LawfulMonad m] (interp : {ι : Type u} → F ι → m ι) (x : FreeM F (α → β)) (y : FreeM F α) : (x <*> y).liftM interp = x.liftM interp <*> y.liftM interp := by simp [seq_eq_bind_map]
209
leanexplore_declaration
leanexplore:386963
leanexplore_sqlite
leanexplore_declarations
@[simp] lemma liftM_seqLeft [LawfulMonad m] (interp : {ι : Type u} → F ι → m ι) (x : FreeM F α) (y : FreeM F β) : (x <* y).liftM interp = x.liftM interp <* y.liftM interp := by simp [seqLeft_eq_bind]
211
leanexplore_declaration
leanexplore:386964
leanexplore_sqlite
leanexplore_declarations
@[simp] lemma liftM_seqRight [LawfulMonad m] (interp : {ι : Type u} → F ι → m ι) (x : FreeM F α) (y : FreeM F β) : (x *> y).liftM interp = x.liftM interp *> y.liftM interp := by simp [seqRight_eq_bind]
730
leanexplore_declaration
leanexplore:386965
leanexplore_sqlite
leanexplore_declarations
/-- A predicate stating that `interp : FreeM F α → m α` is an interpreter for the effect handler `handler : ∀ {α}, F α → m α`. This means that `interp` is a monad morphism from the free monad `FreeM F` to the monad `m`, and that it extends the interpretation of individual operations given by `f`. Formally, `interp` s...
306
leanexplore_declaration
leanexplore:386966
leanexplore_sqlite
leanexplore_declarations
theorem Interprets.eq {handler : {ι : Type u} → F ι → m ι} {interp : FreeM F α → m α} (h : Interprets handler interp) : interp = (·.liftM @handler) := by ext x induction x with | pure a => exact h.apply_pure a | liftBind op cont ih => rw [liftM_liftBind, h.apply_liftBind] simp [ih]
176
leanexplore_declaration
leanexplore:386967
leanexplore_sqlite
leanexplore_declarations
theorem Interprets.liftM (handler : {ι : Type u} → F ι → m ι) : Interprets handler (·.liftM handler : FreeM F α → _) where apply_pure _ := rfl apply_liftBind _ _ := rfl
397
leanexplore_declaration
leanexplore:386968
leanexplore_sqlite
leanexplore_declarations
/-- The universal property of the free monad `FreeM`. That is, `liftM handler` is the unique interpreter that extends the effect handler `handler` to interpret `FreeM F` computations in a monad `m`. -/ theorem Interprets.iff (handler : {ι : Type u} → F ι → m ι) (interp : FreeM F α → m α) : Interprets handler inter...
205
leanexplore_declaration
leanexplore:386969
leanexplore_sqlite
leanexplore_declarations
/-- Type constructor for state operations. -/ inductive StateF (σ : Type u) : Type u → Type u where /-- Get the current state. -/ | get : StateF σ σ /-- Set the state. -/ | set : σ → StateF σ PUnit
50
leanexplore_declaration
leanexplore:386970
leanexplore_sqlite
leanexplore_declarations
/-- Get the current state. -/ | get : StateF σ σ
50
leanexplore_declaration
leanexplore:386971
leanexplore_sqlite
leanexplore_declarations
/-- Set the state. -/ | set : σ → StateF σ PUnit
91
leanexplore_declaration
leanexplore:386972
leanexplore_sqlite
leanexplore_declarations
/-- State monad via the `FreeM` monad. -/ abbrev FreeState (σ : Type u) := FreeM (StateF σ)
254
leanexplore_declaration
leanexplore:386973
leanexplore_sqlite
leanexplore_declarations
instance : MonadStateOf σ (FreeState σ) where get := .lift .get set newState := .liftBind (.set newState) (fun _ => .pure PUnit.unit) modifyGet f := .liftBind .get (fun s => let (a, s') := f s .liftBind (.set s') (fun _ => .pure a))
65
leanexplore_declaration
leanexplore:386974
leanexplore_sqlite
leanexplore_declarations
@[simp] lemma get_def : (get : FreeState σ σ) = .lift .get := rfl
83
leanexplore_declaration
leanexplore:386975
leanexplore_sqlite
leanexplore_declarations
@[simp] lemma set_def (s : σ) : (set s : FreeState σ PUnit) = .lift (.set s) := rfl
168
leanexplore_declaration
leanexplore:386976
leanexplore_sqlite
leanexplore_declarations
/-- Interpret `StateF` operations into `StateM`. -/ def stateInterp {α : Type u} : StateF σ α → StateM σ α | .get => MonadStateOf.get | .set s => MonadStateOf.set s
215
leanexplore_declaration
leanexplore:386977
leanexplore_sqlite
leanexplore_declarations
/-- Convert a `FreeState` computation into a `StateM` computation. This is the canonical interpreter derived from `liftM`. -/ def toStateM {α : Type u} (comp : FreeState σ α) : StateM σ α := comp.liftM stateInterp
196
leanexplore_declaration
leanexplore:386978
leanexplore_sqlite
leanexplore_declarations
/-- `toStateM` is the unique interpreter extending `stateInterp`. -/ theorem toStateM_unique {α : Type u} (g : FreeState σ α → StateM σ α) (h : Interprets stateInterp g) : g = toStateM := h.eq
267
leanexplore_declaration
leanexplore:386979
leanexplore_sqlite
leanexplore_declarations
/-- Run a state computation, returning both the result and final state. -/ def run (comp : FreeState σ α) (s₀ : σ) : α × σ := match comp with | .pure a => (a, s₀) | .liftBind StateF.get k => run (k s₀) s₀ | .liftBind (StateF.set s') k => run (k PUnit.unit) s'
377
leanexplore_declaration
leanexplore:386980
leanexplore_sqlite
leanexplore_declarations
/-- The canonical interpreter `toStateM` derived from `liftM` agrees with the hand-written recursive interpreter `run` for `FreeState`. -/ @[simp] theorem run_toStateM {α : Type u} (comp : FreeState σ α) (s₀ : σ) : (toStateM comp).run s₀ = pure (run comp s₀) := by induction comp generalizing s₀ with | pure a =>...
95
leanexplore_declaration
leanexplore:386981
leanexplore_sqlite
leanexplore_declarations
@[simp] lemma run_pure (a : α) (s₀ : σ) : run (.pure a : FreeState σ α) s₀ = (a, s₀) := rfl
108
leanexplore_declaration
leanexplore:386982
leanexplore_sqlite
leanexplore_declarations
@[simp] lemma run_get (k : σ → FreeState σ α) (s₀ : σ) : run (liftBind .get k) s₀ = run (k s₀) s₀ := rfl
129
leanexplore_declaration
leanexplore:386983
leanexplore_sqlite
leanexplore_declarations
@[simp] lemma run_set (s' : σ) (k : PUnit → FreeState σ α) (s₀ : σ) : run (liftBind (.set s') k) s₀ = run (k .unit) s' := rfl
284
leanexplore_declaration
leanexplore:386984
leanexplore_sqlite
leanexplore_declarations
@[simp] lemma run_bind (x : FreeState σ α) (f : α → FreeState σ β) (s₀ : σ) : run (x.bind f) s₀ = let p := x.run s₀; (f p.1).run p.2 := by induction x generalizing f s₀ with | pure => simp | liftBind op cont ih => rw [FreeM.liftBind_bind] cases op <;> simp [run, ih]
116
leanexplore_declaration
leanexplore:386985
leanexplore_sqlite
leanexplore_declarations
/-- Run a state computation, returning only the result. -/ def run' (c : FreeState σ α) (s₀ : σ) : α := (run c s₀).1
172
leanexplore_declaration
leanexplore:386986
leanexplore_sqlite
leanexplore_declarations
theorem run'_toStateM {α : Type u} (comp : FreeState σ α) (s₀ : σ) : (toStateM comp).run' s₀ = pure (run' comp s₀) := by rw [run', StateT.run'_eq, run_toStateM] rfl
91
leanexplore_declaration
leanexplore:386987
leanexplore_sqlite
leanexplore_declarations
@[simp] lemma run'_pure (a : α) (s₀ : σ) : run' (.pure a : FreeState σ α) s₀ = a := rfl
111
leanexplore_declaration
leanexplore:386988
leanexplore_sqlite
leanexplore_declarations
@[simp] lemma run'_get (k : σ → FreeState σ α) (s₀ : σ) : run' (liftBind .get k) s₀ = run' (k s₀) s₀ := rfl
132
leanexplore_declaration
leanexplore:386989
leanexplore_sqlite
leanexplore_declarations
@[simp] lemma run'_set (s' : σ) (k : PUnit → FreeState σ α) (s₀ : σ) : run' (liftBind (.set s') k) s₀ = run' (k .unit) s' := rfl
181
leanexplore_declaration
leanexplore:386990
leanexplore_sqlite
leanexplore_declarations
@[simp] lemma run'_bind (x : FreeState σ α) (f : α → FreeState σ β) (s₀ : σ) : run' (x.bind f) s₀ = let p := x.run s₀; (f p.1).run' p.2 := congr_arg Prod.fst <| run_bind _ _ _
233
leanexplore_declaration
leanexplore:386991
leanexplore_sqlite
leanexplore_declarations
/-- Type constructor for writer operations. Writer has a single effect, so the definition has just one constructor. -/ inductive WriterF (ω : Type u) : Type v → Type u /-- Write a value to the log. -/ | tell : ω → WriterF ω PUnit
63
leanexplore_declaration
leanexplore:386992
leanexplore_sqlite
leanexplore_declarations
/-- Write a value to the log. -/ | tell : ω → WriterF ω PUnit
241
leanexplore_declaration
leanexplore:386993
leanexplore_sqlite
leanexplore_declarations
/-- Writer monad implemented via the `FreeM` monad construction. This provides a more efficient implementation than the traditional `WriterT` transformer, as it avoids buffering the log. -/ abbrev FreeWriter (ω : Type u) := FreeM (WriterF ω)
148
leanexplore_declaration
leanexplore:386994
leanexplore_sqlite
leanexplore_declarations
/-- Interpret `WriterF` operations into `WriterT`. -/ def writerInterp {α : Type u} : WriterF ω α → WriterT ω Id α | .tell w => MonadWriter.tell w
235
leanexplore_declaration
leanexplore:386995
leanexplore_sqlite
leanexplore_declarations
/-- Convert a `FreeWriter` computation into a `WriterT` computation. This is the canonical interpreter derived from `liftM`. -/ def toWriterT {α : Type u} [Monoid ω] (comp : FreeWriter ω α) : WriterT ω Id α := comp.liftM writerInterp
217
leanexplore_declaration
leanexplore:386996
leanexplore_sqlite
leanexplore_declarations
/-- `toWriterT` is the unique interpreter extending `writerInterp`. -/ theorem toWriterT_unique {α : Type u} [Monoid ω] (g : FreeWriter ω α → WriterT ω Id α) (h : Interprets writerInterp g) : g = toWriterT := h.eq
143
leanexplore_declaration
leanexplore:386997
leanexplore_sqlite
leanexplore_declarations
/-- Writes a log entry. This creates an effectful node in the computation tree. -/ abbrev tell (w : ω) : FreeWriter ω PUnit := lift (.tell w)
68
leanexplore_declaration
leanexplore:386998
leanexplore_sqlite
leanexplore_declarations
@[simp] lemma tell_def (w : ω) : tell w = .lift (.tell w) := rfl
341
leanexplore_declaration
leanexplore:386999
leanexplore_sqlite
leanexplore_declarations
/-- Interprets a `FreeWriter` computation by recursively traversing the tree, accumulating log entries with the monoid operation, and returns the final value paired with the accumulated log. -/ def run [Monoid ω] : FreeWriter ω α → α × ω | .pure a => (a, 1) | .liftBind (.tell w) k => let (a, w') := run (k .un...
94
leanexplore_declaration
leanexplore:387000
leanexplore_sqlite
leanexplore_declarations
@[simp] lemma run_pure [Monoid ω] (a : α) : run (.pure a : FreeWriter ω α) = (a, 1) := rfl
311
leanexplore_declaration
leanexplore:387001
leanexplore_sqlite
leanexplore_declarations
@[simp] lemma run_bind [Monoid ω] (x : FreeWriter ω α) (f : α → FreeWriter ω β) : run (x.bind f) = let p := run x; ((f p.1).run.1, p.2 * (f p.1).run.2) := by induction x generalizing f with | pure => simp | liftBind op cont ih => rw [FreeM.liftBind_bind] cases op simp [run, ih, mul_assoc]
165
leanexplore_declaration
leanexplore:387002
leanexplore_sqlite
leanexplore_declarations
@[simp] lemma run_liftBind_tell [Monoid ω] (w : ω) (k : PUnit → FreeWriter ω α) : run (liftBind (.tell w) k) = (let (a, w') := run (k .unit); (a, w * w')) := rfl
554
leanexplore_declaration
leanexplore:387003
leanexplore_sqlite
leanexplore_declarations
/-- The canonical interpreter `toWriterT` derived from `liftM` agrees with the hand-written recursive interpreter `run` for `FreeWriter`. -/ @[simp] theorem run_toWriterT {α : Type u} [Monoid ω] (comp : FreeWriter ω α) : (toWriterT comp).run = pure (run comp) := by ext : 1 induction comp with | pure _ => simp...
412
leanexplore_declaration
leanexplore:387004
leanexplore_sqlite
leanexplore_declarations
/-- `listen` captures the log produced by a subcomputation incrementally. It traverses the computation, emitting log entries as encountered, and returns the accumulated log as a result. -/ def listen [Monoid ω] : FreeWriter ω α → FreeWriter ω (α × ω) | .pure a => .pure (a, 1) | .liftBind (.tell w) k => liftBi...
106
leanexplore_declaration
leanexplore:387005
leanexplore_sqlite
leanexplore_declarations
@[simp] lemma listen_pure [Monoid ω] (a : α) : listen (.pure a : FreeWriter ω α) = .pure (a, 1) := rfl
243
leanexplore_declaration
leanexplore:387006
leanexplore_sqlite
leanexplore_declarations
@[simp] lemma listen_liftBind_tell [Monoid ω] (w : ω) (k : PUnit → FreeWriter ω α) : listen (liftBind (.tell w) k) = liftBind (.tell w) (fun _ => listen (k .unit) >>= fun (a, w') => pure (a, w * w')) := by rfl
348
leanexplore_declaration
leanexplore:387007
leanexplore_sqlite
leanexplore_declarations
/-- `pass` allows a subcomputation to modify its own log. After traversing the computation and accumulating its log, the resulting function is applied to rewrite the accumulated log before re-emission. -/ def pass [Monoid ω] (m : FreeWriter ω (α × (ω → ω))) : FreeWriter ω α := let ((a, f), w) := run m liftBind (.te...
154
leanexplore_declaration
leanexplore:387008
leanexplore_sqlite
leanexplore_declarations
@[simp] lemma pass_def [Monoid ω] (m : FreeWriter ω (α × (ω → ω))) : pass m = let ((a, f), w) := run m; liftBind (.tell (f w)) fun _ => .pure a := rfl
105
leanexplore_declaration
leanexplore:387009
leanexplore_sqlite
leanexplore_declarations
instance [Monoid ω] : MonadWriter ω (FreeWriter ω) where tell := tell listen := listen pass := pass
225
leanexplore_declaration
leanexplore:387010
leanexplore_sqlite
leanexplore_declarations
/-- Type constructor for continuation operations. -/ inductive ContF (r : Type u) (α : Type v) where /-- Call with current continuation: provides access to the current continuation. -/ | callCC : ((α → r) → r) → ContF r α
122
leanexplore_declaration
leanexplore:387011
leanexplore_sqlite
leanexplore_declarations
/-- Call with current continuation: provides access to the current continuation. -/ | callCC : ((α → r) → r) → ContF r α
99
leanexplore_declaration
leanexplore:387012
leanexplore_sqlite
leanexplore_declarations
instance {r : Type u} : Functor (ContF r) where map f | .callCC g => .callCC fun k => g (k ∘ f)
96
leanexplore_declaration
leanexplore:387013
leanexplore_sqlite
leanexplore_declarations
/-- Continuation monad via the `FreeM` monad. -/ abbrev FreeCont (r : Type u) := FreeM (ContF r)
117
leanexplore_declaration
leanexplore:387014
leanexplore_sqlite
leanexplore_declarations
/-- Interpret `ContF r` operations into `ContT r Id`. -/ def contInterp : ContF r α → ContT r Id α | .callCC g => g
212
leanexplore_declaration
leanexplore:387015
leanexplore_sqlite
leanexplore_declarations
/-- Convert a `FreeCont` computation into a `ContT` computation. This is the canonical interpreter derived from `liftM`. -/ def toContT {α : Type u} (comp : FreeCont r α) : ContT r Id α := comp.liftM contInterp