Add documentation to some modules.
This commit is contained in:
@@ -1,7 +1,17 @@
|
|||||||
import Mathlib.Tactic.TypeStar
|
import Mathlib.Tactic.TypeStar
|
||||||
|
|
||||||
|
/-!
|
||||||
|
|
||||||
|
Interpretation to a semantic domain.
|
||||||
|
|
||||||
|
This file serves to introduce the double-angle-bracket "denotation"
|
||||||
|
notation by prodiving a class instance `Interp`, whose single
|
||||||
|
method `interp` is what the double brackets map to. -/
|
||||||
|
|
||||||
namespace Spa
|
namespace Spa
|
||||||
|
|
||||||
|
/-- A type `α` that implements this class has denotation / meaning
|
||||||
|
in the semantic domain `dom`. -/
|
||||||
class Interp (α : Type*) (dom : outParam Type*) where
|
class Interp (α : Type*) (dom : outParam Type*) where
|
||||||
interp : α → dom
|
interp : α → dom
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,19 @@
|
|||||||
import Mathlib.Data.Finset.Basic
|
import Mathlib.Data.Finset.Basic
|
||||||
|
|
||||||
|
/-!
|
||||||
|
|
||||||
|
Base language.
|
||||||
|
|
||||||
|
This file defines the core object language for the program analysis and
|
||||||
|
transformation. It's a very basic imperative language. The `Spa/Language/Tagged/Basic.lean`
|
||||||
|
file provides an auto-derived version of the `Expr`, `BasicStmt`, and `Stmt` data
|
||||||
|
types with unique IDs per condtructor, enabling in-AST pointers.
|
||||||
|
|
||||||
|
-/
|
||||||
|
|
||||||
namespace Spa
|
namespace Spa
|
||||||
|
|
||||||
|
/-- A value-producing expression. Currently, this cannot have side effects. -/
|
||||||
inductive Expr where
|
inductive Expr where
|
||||||
| add (e₁ e₂ : Expr)
|
| add (e₁ e₂ : Expr)
|
||||||
| sub (e₁ e₂ : Expr)
|
| sub (e₁ e₂ : Expr)
|
||||||
@@ -9,11 +21,15 @@ inductive Expr where
|
|||||||
| num (n : ℕ)
|
| num (n : ℕ)
|
||||||
deriving DecidableEq
|
deriving DecidableEq
|
||||||
|
|
||||||
|
/-- A statement that cannot alter control flow (and thus, can be part of a basic block).
|
||||||
|
|
||||||
|
This differs from, e.g., a loop, which can cause execution to jump to its top several times. -/
|
||||||
inductive BasicStmt where
|
inductive BasicStmt where
|
||||||
| assign (x : String) (e : Expr)
|
| assign (x : String) (e : Expr)
|
||||||
| noop
|
| noop
|
||||||
deriving DecidableEq
|
deriving DecidableEq
|
||||||
|
|
||||||
|
/-- Any statements, which may or may not change program state (variable assignments). -/
|
||||||
inductive Stmt where
|
inductive Stmt where
|
||||||
| basic (bs : BasicStmt)
|
| basic (bs : BasicStmt)
|
||||||
| andThen (s₁ s₂ : Stmt)
|
| andThen (s₁ s₂ : Stmt)
|
||||||
@@ -21,16 +37,19 @@ inductive Stmt where
|
|||||||
| whileLoop (e : Expr) (s : Stmt)
|
| whileLoop (e : Expr) (s : Stmt)
|
||||||
deriving DecidableEq
|
deriving DecidableEq
|
||||||
|
|
||||||
|
/-- Variables mentioned in this expression. -/
|
||||||
def Expr.vars : Expr → Finset String
|
def Expr.vars : Expr → Finset String
|
||||||
| .add l r => l.vars ∪ r.vars
|
| .add l r => l.vars ∪ r.vars
|
||||||
| .sub l r => l.vars ∪ r.vars
|
| .sub l r => l.vars ∪ r.vars
|
||||||
| .var s => {s}
|
| .var s => {s}
|
||||||
| .num _ => ∅
|
| .num _ => ∅
|
||||||
|
|
||||||
|
/-- Variables assigned or mentioned in this basic statement. -/
|
||||||
def BasicStmt.vars : BasicStmt → Finset String
|
def BasicStmt.vars : BasicStmt → Finset String
|
||||||
| .assign x e => {x} ∪ e.vars
|
| .assign x e => {x} ∪ e.vars
|
||||||
| .noop => ∅
|
| .noop => ∅
|
||||||
|
|
||||||
|
/-- Variables assigned or mentioned in this statement. -/
|
||||||
def Stmt.vars : Stmt → Finset String
|
def Stmt.vars : Stmt → Finset String
|
||||||
| .basic bs => bs.vars
|
| .basic bs => bs.vars
|
||||||
| .andThen s₁ s₂ => s₁.vars ∪ s₂.vars
|
| .andThen s₁ s₂ => s₁.vars ∪ s₂.vars
|
||||||
|
|||||||
@@ -1,8 +1,20 @@
|
|||||||
import Mathlib.Order.Lattice
|
import Mathlib.Order.Lattice
|
||||||
import Mathlib.Order.RelSeries
|
import Mathlib.Order.RelSeries
|
||||||
|
|
||||||
|
/-!
|
||||||
|
|
||||||
|
Lattice Definitions.
|
||||||
|
|
||||||
|
This file provides some definitions for lattices. It used to be more critical
|
||||||
|
when this was an Agda project, since it defined (semi)lattices, the ordering
|
||||||
|
relation, etc. However, these have been lifted into `Mathlib.Order.Lattice`
|
||||||
|
etc.. What remains are a couple of theorems about folds, as well
|
||||||
|
as `FiniteHeightLattice`, the core concept of lattice-based static
|
||||||
|
program analyses. See the documentation on that class for more information. -/
|
||||||
|
|
||||||
namespace Spa
|
namespace Spa
|
||||||
|
|
||||||
|
/-- Predicate for binary functions independently monotone in both their arguments. -/
|
||||||
def Monotone₂ {α β γ : Type*} [Preorder α] [Preorder β] [Preorder γ]
|
def Monotone₂ {α β γ : Type*} [Preorder α] [Preorder β] [Preorder γ]
|
||||||
(f : α → β → γ) : Prop :=
|
(f : α → β → γ) : Prop :=
|
||||||
(∀ b, Monotone (f · b)) ∧ (∀ a, Monotone (f a ·))
|
(∀ b, Monotone (f · b)) ∧ (∀ a, Monotone (f a ·))
|
||||||
@@ -11,18 +23,20 @@ section Folds
|
|||||||
|
|
||||||
variable {α β : Type*} [Preorder α] [Preorder β]
|
variable {α β : Type*} [Preorder α] [Preorder β]
|
||||||
|
|
||||||
|
/-- (right) folds are monotonic in both their arguments if the underlying accumulator function is. -/
|
||||||
lemma foldr_mono {l₁ l₂ : List α} (f : α → β → β) {b₁ b₂ : β}
|
lemma foldr_mono {l₁ l₂ : List α} (f : α → β → β) {b₁ b₂ : β}
|
||||||
(hl : List.Forall₂ (· ≤ ·) l₁ l₂) (hb : b₁ ≤ b₂)
|
(hl : List.Forall₂ (· ≤ ·) l₁ l₂) (hb : b₁ ≤ b₂)
|
||||||
(hf₁ : ∀ b, Monotone fun a => f a b) (hf₂ : ∀ a, Monotone (f a)) :
|
(hf₁ : ∀ b, Monotone (f · b)) (hf₂ : ∀ a, Monotone (f a ·)) :
|
||||||
l₁.foldr f b₁ ≤ l₂.foldr f b₂ := by
|
l₁.foldr f b₁ ≤ l₂.foldr f b₂ := by
|
||||||
induction hl with
|
induction hl with
|
||||||
| nil => exact hb
|
| nil => exact hb
|
||||||
| cons hxy _ ih =>
|
| cons hxy _ ih =>
|
||||||
exact le_trans (hf₁ _ hxy) (hf₂ _ ih)
|
exact le_trans (hf₁ _ hxy) (hf₂ _ ih)
|
||||||
|
|
||||||
|
/-- (left) folds are monotinic in both their arguments if the underlying accumulator function is. -/
|
||||||
lemma foldl_mono {l₁ l₂ : List α} (f : β → α → β) {b₁ b₂ : β}
|
lemma foldl_mono {l₁ l₂ : List α} (f : β → α → β) {b₁ b₂ : β}
|
||||||
(hl : List.Forall₂ (· ≤ ·) l₁ l₂) (hb : b₁ ≤ b₂)
|
(hl : List.Forall₂ (· ≤ ·) l₁ l₂) (hb : b₁ ≤ b₂)
|
||||||
(hf₁ : ∀ a, Monotone fun b => f b a) (hf₂ : ∀ b, Monotone (f b)) :
|
(hf₁ : ∀ a, Monotone (f · a)) (hf₂ : ∀ b, Monotone (f b ·)) :
|
||||||
l₁.foldl f b₁ ≤ l₂.foldl f b₂ := by
|
l₁.foldl f b₁ ≤ l₂.foldl f b₂ := by
|
||||||
induction hl generalizing b₁ b₂ with
|
induction hl generalizing b₁ b₂ with
|
||||||
| nil => exact hb
|
| nil => exact hb
|
||||||
@@ -30,14 +44,16 @@ lemma foldl_mono {l₁ l₂ : List α} (f : β → α → β) {b₁ b₂ : β}
|
|||||||
exact ih (le_trans (hf₁ _ hb) (hf₂ _ hxy))
|
exact ih (le_trans (hf₁ _ hb) (hf₂ _ hxy))
|
||||||
|
|
||||||
omit [Preorder α] in
|
omit [Preorder α] in
|
||||||
|
/-- (right) folds on a particular list are monotonic if the underlying accumulator is monotonic in its accumulator argument. -/
|
||||||
lemma foldr_mono' (l : List α) (f : α → β → β)
|
lemma foldr_mono' (l : List α) (f : α → β → β)
|
||||||
(hf : ∀ a, Monotone (f a ·)) : Monotone fun b => l.foldr f b := by
|
(hf : ∀ a, Monotone (f a ·)) : Monotone (l.foldr f ·) := by
|
||||||
intro b₁ b₂ hb
|
intro b₁ b₂ hb
|
||||||
induction l with
|
induction l with
|
||||||
| nil => exact hb
|
| nil => exact hb
|
||||||
| cons x xs ih => exact hf x ih
|
| cons x xs ih => exact hf x ih
|
||||||
|
|
||||||
omit [Preorder α] in
|
omit [Preorder α] in
|
||||||
|
/-- (left) folds on a particular list are monotonic if the underlying accumulator is monotonic in its accumulator argument. -/
|
||||||
lemma foldl_mono' (l : List α) (f : β → α → β)
|
lemma foldl_mono' (l : List α) (f : β → α → β)
|
||||||
(hf : ∀ a, Monotone (f · a)) : Monotone fun b => l.foldl f b := by
|
(hf : ∀ a, Monotone (f · a)) : Monotone fun b => l.foldl f b := by
|
||||||
intro b₁ b₂ hb
|
intro b₁ b₂ hb
|
||||||
@@ -47,15 +63,18 @@ lemma foldl_mono' (l : List α) (f : β → α → β)
|
|||||||
|
|
||||||
end Folds
|
end Folds
|
||||||
|
|
||||||
|
/-- Predicate on types with `Preorder` that claims all $<$ chains in the type have at most `n` comparisons. -/
|
||||||
def BoundedChains (α : Type*) [Preorder α] (n : ℕ) : Prop :=
|
def BoundedChains (α : Type*) [Preorder α] (n : ℕ) : Prop :=
|
||||||
∀ c : LTSeries α, c.length ≤ n
|
∀ c : LTSeries α, c.length ≤ n
|
||||||
|
|
||||||
|
/-- Wrapper over `LTSeries` that exposes its beginning and end points, as well as its length, as part of the type. -/
|
||||||
structure PointedLTSeries (α : Type*) (f t : α) (n : ℕ) [Preorder α] where
|
structure PointedLTSeries (α : Type*) (f t : α) (n : ℕ) [Preorder α] where
|
||||||
series : LTSeries α
|
series : LTSeries α
|
||||||
head_series : series.head = f
|
head_series : series.head = f
|
||||||
last_series : series.last = t
|
last_series : series.last = t
|
||||||
length_series : series.length = n
|
length_series : series.length = n
|
||||||
|
|
||||||
|
/-- A finite height lattice is a lattice in which all chains $a < \ldots < z$ have a maximum height `height`. -/
|
||||||
class FiniteHeightLattice (α : Type*) [Lattice α] extends Bot α, Top α where
|
class FiniteHeightLattice (α : Type*) [Lattice α] extends Bot α, Top α where
|
||||||
height : ℕ
|
height : ℕ
|
||||||
longestChain : PointedLTSeries α ⊥ ⊤ height
|
longestChain : PointedLTSeries α ⊥ ⊤ height
|
||||||
@@ -65,6 +84,7 @@ namespace FiniteHeightLattice
|
|||||||
|
|
||||||
variable (α : Type*) [Lattice α] [FiniteHeightLattice α]
|
variable (α : Type*) [Lattice α] [FiniteHeightLattice α]
|
||||||
|
|
||||||
|
/-- The bottom element `⊥` of a finite height lattice is _actually_ the least element. -/
|
||||||
lemma bot_le (a : α) : (⊥ : α) ≤ a := by
|
lemma bot_le (a : α) : (⊥ : α) ≤ a := by
|
||||||
by_cases heq : ⊥ ⊓ a = ⊥
|
by_cases heq : ⊥ ⊓ a = ⊥
|
||||||
· exact inf_eq_left.mp heq
|
· exact inf_eq_left.mp heq
|
||||||
|
|||||||
Reference in New Issue
Block a user