Extract 'monotonic state' into its own module

Signed-off-by: Danila Fedorin <danila.fedorin@gmail.com>
This commit is contained in:
2024-04-13 20:46:30 -07:00
parent fc27b045d3
commit 7571cb7451
2 changed files with 118 additions and 88 deletions

View File

@@ -110,8 +110,7 @@ cast∈⇒∈subst refl refl (idx₁ , idx₂) es e∈es
(e∈g₂⇒e∈g₃ (e∈g₁⇒e∈g₂ e∈g₁)))
}
record Relaxable (T : Graph → Set) : Set where
field relax : ∀ {g₁ g₂ : Graph} → g₁ ⊆ g₂ → T g₁ → T g₂
open import MonotonicState _⊆_ ⊆-trans renaming (MonotonicState to MonotonicGraphFunction)
instance
IndexRelaxable : Relaxable Graph.Index
@@ -127,17 +126,7 @@ instance
)
}
ProdRelaxable : ∀ {P : Graph → Set} {Q : Graph → Set} →
{{ PRelaxable : Relaxable P }} → {{ QRelaxable : Relaxable Q }} →
Relaxable (P ⊗ Q)
ProdRelaxable {{pr}} {{qr}} = record
{ relax = (λ { g₁⊆g₂ (p , q) →
( Relaxable.relax pr g₁⊆g₂ p
, Relaxable.relax qr g₁⊆g₂ q) }
)
}
open Relaxable {{...}} public
open Relaxable {{...}}
relax-preserves-[]≡ : ∀ (g₁ g₂ : Graph) (g₁⊆g₂ : g₁ ⊆ g₂) (idx : Graph.Index g₁) →
g₁ [ idx ] ≡ g₂ [ relax g₁⊆g₂ idx ]
@@ -145,81 +134,6 @@ relax-preserves-[]≡ g₁ g₂ (Mk-⊆ n refl newNodes nsg₂≡nsg₁++newNode
rewrite cast-is-id refl (Graph.nodes g₂)
with refl ← nsg₂≡nsg₁++newNodes = sym (lookup-++ˡ (Graph.nodes g₁) _ _)
-- Tools for graph construction. The most important is a 'monotonic function':
-- one that takes a graph, and produces another graph, such that the
-- new graph includes all the information from the old one.
MonotonicGraphFunction : (Graph → Set) → Set
MonotonicGraphFunction T = (g₁ : Graph) → Σ Graph (λ g₂ → T g₂ × g₁ ⊆ g₂)
-- Now, define some operations on monotonic functions; these are useful
-- to save the work of threading intermediate graphs in and out of operations.
infixr 2 _⟨⊗⟩_
_⟨⊗⟩_ : ∀ {T₁ T₂ : Graph → Set} {{ T₁Relaxable : Relaxable T₁ }} →
MonotonicGraphFunction T₁ → MonotonicGraphFunction T₂ →
MonotonicGraphFunction (T₁ ⊗ T₂)
_⟨⊗⟩_ {{r}} f₁ f₂ g
with (g' , (t₁ , g⊆g')) ← f₁ g
with (g'' , (t₂ , g'⊆g'')) ← f₂ g' =
(g'' , ((Relaxable.relax r g'⊆g'' t₁ , t₂) , ⊆-trans g⊆g' g'⊆g''))
infixl 2 _update_
_update_ : ∀ {T : Graph → Set} {{ TRelaxable : Relaxable T }} →
MonotonicGraphFunction T → (∀ (g : Graph) → T g → Σ Graph (λ g' → g ⊆ g')) →
MonotonicGraphFunction T
_update_ {{r}} f mod g
with (g' , (t , g⊆g')) ← f g
with (g'' , g'⊆g'') ← mod g' t =
(g'' , ((Relaxable.relax r g'⊆g'' t , ⊆-trans g⊆g' g'⊆g'')))
infixl 2 _map_
_map_ : ∀ {T₁ T₂ : Graph → Set} →
MonotonicGraphFunction T₁ → (∀ (g : Graph) → T₁ g → T₂ g) →
MonotonicGraphFunction T₂
_map_ f fn g = let (g' , (t₁ , g⊆g')) = f g in (g' , (fn g' t₁ , g⊆g'))
-- To reason about monotonic functions and what we do, we need a way
-- to describe values they produce. A 'graph-value predicate' is
-- just a predicate for some (dependent) value.
GraphValuePredicate : (Graph → Set) → Set₁
GraphValuePredicate T = ∀ (g : Graph) → T g → Set
Both : {T₁ T₂ : Graph → Set} → GraphValuePredicate T₁ → GraphValuePredicate T₂ →
GraphValuePredicate (T₁ ⊗ T₂)
Both P Q = (λ { g (t₁ , t₂) → (P g t₁ × Q g t₂) })
-- Since monotnic functions keep adding on to a function, proofs of
-- graph-value predicates go stale fast (they describe old values of
-- the graph). To keep propagating them through, we need them to still
-- on 'bigger graphs'. We call such predicates monotonic as well, since
-- they respect the ordering of graphs.
MonotonicPredicate : ∀ {T : Graph → Set} {{ TRelaxable : Relaxable T }} →
GraphValuePredicate T → Set
MonotonicPredicate {T} P = ∀ (g₁ g₂ : Graph) (t₁ : T g₁) (g₁⊆g₂ : g₁ ⊆ g₂) →
P g₁ t₁ → P g₂ (relax g₁⊆g₂ t₁)
-- A 'map' has a certain property if its ouputs satisfy that property
-- for all inputs.
always : ∀ {T : Graph → Set} → GraphValuePredicate T → MonotonicGraphFunction T → Set
always P m = ∀ g₁ → let (g₂ , t , _) = m g₁ in P g₂ t
⟨⊗⟩-reason : ∀ {T₁ T₂ : Graph → Set} {{ T₁Relaxable : Relaxable T₁ }}
{P : GraphValuePredicate T₁} {Q : GraphValuePredicate T₂}
{P-Mono : MonotonicPredicate P}
{m₁ : MonotonicGraphFunction T₁} {m₂ : MonotonicGraphFunction T₂} →
always P m₁ → always Q m₂ → always (Both P Q) (m₁ ⟨⊗⟩ m₂)
⟨⊗⟩-reason {P-Mono = P-Mono} {m₁ = m₁} {m₂ = m₂} aP aQ g
with p ← aP g
with (g' , (t₁ , g⊆g')) ← m₁ g
with q ← aQ g'
with (g'' , (t₂ , g'⊆g'')) ← m₂ g' = (P-Mono _ _ _ g'⊆g'' p , q)
pushBasicBlock : List BasicStmt → MonotonicGraphFunction Graph.Index
pushBasicBlock bss g =
( record