integrity-constraint-governance / ControlFlowInvariants.agda
SNAPKITTYWEST's picture
push from SNAPKITTYWEST/integrity-constraint-governance
756f45d verified
Raw History Blame Contribute Delete
6.32 kB
module ControlFlowInvariants where
open import Relation.Binary.PropositionalEquality using (_≑_; refl)
open import Data.Product using (_Γ—_; _,_; proj₁; projβ‚‚)
-- ============================================================
-- Abstract types for the ICP state machine architecture
-- ============================================================
postulate
Label : Set -- node identifiers in the ^ICP DAG
State : Set -- contents of ^ICP globals (memory/environment)
-- ============================================================
-- Control transfer primitives β€” structural duals
-- ============================================================
-- GOTO : push mechanics β€” L1 dictates the future by pushing to L2
-- COME-FROM : pull mechanics β€” L2 dictates the past by pulling from L1
data Transfer : Set where
GOTO : Label β†’ Transfer
COME-FROM : Label β†’ Transfer
-- A Program maps every Label to its outgoing Transfer
Prog = Label β†’ Transfer
-- ============================================================
-- Operational semantics β€” the execution step relation
-- ============================================================
data Step (P : Prog) : Label Γ— State β†’ Label Γ— State β†’ Set where
-- PUSH INVARIANT: L1 actively pushes execution to L2.
-- State is passed through unchanged.
step-goto : βˆ€ {l₁ lβ‚‚ s}
β†’ P l₁ ≑ GOTO lβ‚‚
------------------------------
β†’ Step P (l₁ , s) (lβ‚‚ , s)
-- PULL INVARIANT: L2 actively pulls execution from L1.
-- State is passed through unchanged.
step-come-from : βˆ€ {l₁ lβ‚‚ s}
β†’ P lβ‚‚ ≑ COME-FROM l₁
------------------------------
β†’ Step P (l₁ , s) (lβ‚‚ , s)
-- ============================================================
-- INVARIANT 1: Strict State Preservation
--
-- Pure control-flow graph traversal guarantees the domain (State)
-- is unmutated across the topological shift.
-- This justifies the MUMPS PUSH/PULL routines passing STATE by value
-- and the ASP layer not touching ^ICP("NODE",...) data during traversal.
-- ============================================================
state-invariant : βˆ€ {P l₁ lβ‚‚ s₁ sβ‚‚}
β†’ Step P (l₁ , s₁) (lβ‚‚ , sβ‚‚)
β†’ s₁ ≑ sβ‚‚
state-invariant (step-goto _) = refl
state-invariant (step-come-from _) = refl
-- ============================================================
-- INVARIANT 2: Control-Flow Duality
--
-- A GOTO edge is structurally isomorphic to a COME-FROM edge.
-- is-dual P₁ Pβ‚‚ captures the bidirectional adjacency requirement
-- enforced by the ASP integrity constraints:
-- :- goto(L1,L2,Guard), not come_from(L2,L1,Guard).
-- :- come_from(L2,L1,Guard), not goto(L1,L2,Guard).
-- ============================================================
is-dual : Prog β†’ Prog β†’ Set
is-dual P₁ Pβ‚‚ =
βˆ€ l₁ lβ‚‚
β†’ (P₁ l₁ ≑ GOTO lβ‚‚ β†’ Pβ‚‚ lβ‚‚ ≑ COME-FROM l₁)
Γ— (Pβ‚‚ lβ‚‚ ≑ COME-FROM l₁ β†’ P₁ l₁ ≑ GOTO lβ‚‚)
-- If P₁ has a GOTO step from (l₁,s) to (lβ‚‚,s),
-- then the dual program Pβ‚‚ achieves the identical topological move
-- via COME-FROM β€” the same (Label Γ— State) transition, different mechanics.
duality-invariant : βˆ€ {P₁ Pβ‚‚ l₁ lβ‚‚ s}
β†’ is-dual P₁ Pβ‚‚
β†’ P₁ l₁ ≑ GOTO lβ‚‚
β†’ Step Pβ‚‚ (l₁ , s) (lβ‚‚ , s)
duality-invariant dual p₁-goto =
step-come-from (proj₁ (dual _ _) p₁-goto)
-- Symmetric: a COME-FROM step in Pβ‚‚ corresponds to a GOTO step in P₁.
duality-invariant-sym : βˆ€ {P₁ Pβ‚‚ l₁ lβ‚‚ s}
β†’ is-dual P₁ Pβ‚‚
β†’ Pβ‚‚ lβ‚‚ ≑ COME-FROM l₁
β†’ Step P₁ (l₁ , s) (lβ‚‚ , s)
duality-invariant-sym dual pβ‚‚-come-from =
step-goto (projβ‚‚ (dual _ _) pβ‚‚-come-from)
-- ============================================================
-- INVARIANT 3: Conditional Duality (Guard-tripartite edges)
--
-- When transitions are guarded, both GOTO and COME-FROM must
-- agree on the same guard. This is enforced by the ASP layer:
-- :- goto(L1,L2,Guard), not come_from(L2,L1,Guard).
-- Formalised here as a stronger is-dual over guarded programs.
-- ============================================================
postulate
Guard : Set -- guard conditions (evaluated against State)
-- A guarded transfer carries a condition
data GuardedTransfer : Set where
GOTO-IF : Label β†’ Guard β†’ GuardedTransfer
COME-FROM-IF : Label β†’ Guard β†’ GuardedTransfer
GuardedProg = Label β†’ GuardedTransfer
-- Guarded step: transition fires only when guard matches
data GuardedStep (P : GuardedProg) (holds : Guard β†’ State β†’ Set)
: Label Γ— State β†’ Label Γ— State β†’ Set where
step-goto-guard : βˆ€ {l₁ lβ‚‚ g s}
β†’ P l₁ ≑ GOTO-IF lβ‚‚ g
β†’ holds g s
β†’ GuardedStep P holds (l₁ , s) (lβ‚‚ , s)
step-come-from-guard : βˆ€ {l₁ lβ‚‚ g s}
β†’ P lβ‚‚ ≑ COME-FROM-IF l₁ g
β†’ holds g s
β†’ GuardedStep P holds (l₁ , s) (lβ‚‚ , s)
-- Guarded duality: both programs must use the same guard on the same edge.
is-guarded-dual : GuardedProg β†’ GuardedProg β†’ Set
is-guarded-dual P₁ Pβ‚‚ =
βˆ€ l₁ lβ‚‚ g
β†’ (P₁ l₁ ≑ GOTO-IF lβ‚‚ g β†’ Pβ‚‚ lβ‚‚ ≑ COME-FROM-IF l₁ g)
Γ— (Pβ‚‚ lβ‚‚ ≑ COME-FROM-IF l₁ g β†’ P₁ l₁ ≑ GOTO-IF lβ‚‚ g)
-- Conditional duality invariant:
-- If P₁ fires a guarded GOTO under guard g at state s,
-- the dual Pβ‚‚ fires the identical move via COME-FROM-IF with the same guard.
conditional-duality-invariant : βˆ€ {P₁ Pβ‚‚ holds l₁ lβ‚‚ g s}
β†’ is-guarded-dual P₁ Pβ‚‚
β†’ P₁ l₁ ≑ GOTO-IF lβ‚‚ g
β†’ holds g s
β†’ GuardedStep Pβ‚‚ holds (l₁ , s) (lβ‚‚ , s)
conditional-duality-invariant dual p₁-goto-g hgs =
step-come-from-guard (proj₁ (dual _ _ _) p₁-goto-g) hgs
-- Determinism corollary (mirrors ASP constraint):
-- A state machine is deterministic iff at most one outgoing guarded edge holds.
is-deterministic : GuardedProg β†’ (Guard β†’ State β†’ Set) β†’ Set
is-deterministic P holds =
βˆ€ l₁ lβ‚‚ l₃ g₁ gβ‚‚ s
β†’ P l₁ ≑ GOTO-IF lβ‚‚ g₁
β†’ P l₁ ≑ GOTO-IF l₃ gβ‚‚
β†’ holds g₁ s
β†’ holds gβ‚‚ s
β†’ lβ‚‚ ≑ l₃